Rust从入门到放弃01-建立基于axum的Web请求

Rust从入门到放弃01-建立基于axum的Web请求

本次开一个新坑,原链作者为:https://www.sunzhongwei.com/rust-axum-framework-tutorial,清晰简洁明了

省略Rust环境的安装搭建等

Cargo.toml


[package]
name = "RustWeb"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.6.20"
tokio = { version = "1.16.1", features = ["full"] }
serde_json = "1.0.117"
serde = { version = "1.0.130", features = ["derive"] }

main.rs


use axum::{Router, routing::get, response::{Json, Html,Form} };
// use axum::handler::HandlerWithoutStateExt;
use serde_json::{json, Value};
use serde::Deserialize;
#[tokio::main]
async fn main() {
    // let app = Router::new().route("/", get(|| async { "Hello, World!" }));
    // axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
    //     .serve(app.into_make_service())
    //     .await.unwrap();

    let app = Router::new()
        .route("/", get(hello_txt))
        .route("/json", get(hello_json))
        .route("/html", get(hello_html))
        .route("/form", get(render_form).post(handle_form_submit));
    println!("Server running on http://localhost:3000......");
    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await.unwrap();

}

async fn hello_txt()->&'static str {
    "Hello, World!"
}

async fn hello_json()->Json<Value> {
    Json(json!({"domain":"http://localhost:3000","since":1573}))

}

async fn hello_html()->Html<&'static str> {
    Html(r#" <html>
        <head>
            <title>Form Example</title>
        </head>
        <body>
            <h1>Form Example</h1>
            <form method="post">
                <label for="field1">Field 1:</label>
                <input type="text" name="field1" id="field1"><br>

                <label for="field2">Field 2:</label>
                <input type="text" name="field2" id="field2"><br>

                <input type="submit" value="Submit">
            </form>
        </body>
        </html>"#)
}
async fn render_form()->Html<&'static str> {
    Html(r#" <html>
        <head>
            <title>Form Example</title>
        </head>
        <body>
            <h1>Form Example</h1>
            <form method="post">
                <label for="field1">Field 1:</label>
                <input type="text" name="field1" id="field1"><br>

                <label for="field2">Field 2:</label>
                <input type="text" name="field2" id="field2"><br>

                <input type="submit" value="Submit">
            </form>
        </body>
        </html>"#)
}

#[derive(Deserialize)]
struct FormData {
    field1: String,
    field2: String,
}

async fn handle_form_submit(Form(form_data):Form<FormData>) ->Html<String>{
    let response_html = format!(r#"
     <html>
        <head>
            <title>Form Submission Result</title>
        </head>
        <body>
            <h1>Form Submission Result</h1>
            <p>Field 1: {}</p>
            <p>Field 2: {}</p>
        </body>
        </html>
    "#, form_data.field1, form_data.field2);
    Html(response_html)
}
posted @   ProsperousEnding  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
  1. 1 原来你也在这里 周笔畅
  2. 2 世间美好与你环环相扣 柏松
  3. 3 起风了 吴青峰
  4. 4 极恶都市 夏日入侵企划
  5. 5 所念皆星河 CMJ
  6. 6 所念皆星河-歌词版 房东的猫
  7. 7 卡农-钢琴版 dylanf
  8. 8 The truth that you leave Pianoboy高至豪
  9. 9 虹之间 米叔啊
The truth that you leave - Pianoboy高至豪
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

暂无歌词

点击右上角即可分享
微信分享提示