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 @ 2024-06-21 09:06  ProsperousEnding  阅读(39)  评论(0编辑  收藏  举报