0
点赞
收藏
分享

微信扫一扫

【Rust光年纪】Rust多媒体处理库全面比较:探索安全高效的多媒体处理利器

_铁马冰河_ 2024-08-30 阅读 18

一、Axum

1、toml

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

[dependencies]
tokio = {version="1.39.3",features =["full"]}
axum ={version ="0.7.5",features =["tokio"]}
tower = "0.5.0"
futures = "0.3.30"
serde = {version ="1.0.209",features =["derive"]}
serde_json ="1.0.1"

2、main.rs

use axum::{
    routing::{get,post},
    Router,
    response,
    Form,
    http::{header::HeaderMap},
};
use serde_json::{Value, json};
use serde::Deserialize;
use axum::extract::{ Query,Json,Path};
use std::collections::HashMap;

#[derive(Deserialize,Debug)]
struct User {
    name: String,
    old: i32,
}

#[derive(Deserialize)]
struct LoginForm {
    account: String,
    password: String,
}
#[derive(Deserialize,Debug)]
struct Params {
    tradedays: Option<i32>,
    price:Option<f64>,
    name: Option<String>,
}
#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(hello))
        .route("/headers",get(headers))
        .route("/html", get(hello_html))
        .route("/form", get(show_form).post(accept_form))
        .route("/hashmap", get(query_hashmap))//全部解释成hashmap
        .route("/user/:name/:old", get(query_user)) 
        .route("/path/:name/:id", get(path)) //两个参数
        .route("/json", post(query_user_json))
        .route("/params",get(get_params));
    println!("Serving on http://localhost:8080 ...");
    let listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn headers(headers: HeaderMap) ->String{
    format!("{:?}", headers)
}
// Query: ?和=来解析
async fn get_params(Query(params): Query<Params>) -> String {
    let tradedays = params.tradedays.unwrap_or(0);
    let price = params.price.unwrap_or(0.0);
    let name = params.name.unwrap_or("no name".to_string());
    format!("tradedays {:?}, price: {:?}  name:{:?}", tradedays, price,name)

}

//Json(user):Json<User>
async fn query_hashmap(Query(inputs): Query<HashMap<String, String>>) -> String{
    println!("inputs:{:?}",inputs);
    format!("inputs:{:?}",inputs)
}
async fn query_user(Path(user):Path<User>){
    println!("welcome! user:{user:#?}");
}
async fn query_user_json(Json(user): Json<User>) -> response::Json<Value>{
    println!("response:{:?}",user);
    Json(json!({ "user": user.name, "old": user.old }))

}
// `&'static str` becomes a `200 OK` with `content-type: text/plain; charset=utf-8`
async fn hello() -> &'static str {
    "Hello, World!"
}
async fn path(Path((name,id)):Path<(String,i32)>) ->String{
    println!("hi,{name:#?} => id {id:#?}");
    format!("hi,{},{}",name,id)
    
}
// `Html` will get a `text/html` content-type
async fn hello_html() -> response::Html<&'static str> {
    response::Html("
        <h1>Hello HTML</h1>
        <p>Hello, World!</p>
    ")
}

async fn show_form() -> response::Html<&'static str> {
    response::Html(r#"
        <html>
        <head>
            <title>Login Form</title>
        </head>
        <body>
            <h1>User Info</h1>
            <form method="post">
                <label for="field1">account:</label>
                <input type="text" name="field1" id="field1"><br>

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

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

async fn accept_form(Form(login): Form<LoginForm>) -> response::Html<String> {
    let response_html = format!(
        r#"
        <html>
        <head>
            <title>Form Submission Result</title>
        </head>
        <body>
            <h1>Form Submission Result</h1>
            <p>account : {}</p>
            <p>password: {}</p>
        </body>
        </html>
    "#,
        login.account, login.password);
    
    println!("response_html: {}", response_html);
    response::Html(response_html)
}

二、Rest Client工具协同测试

通过创建以.http 或 .rest为结尾的文件,进行http请求测试。具体如下:


# 头文件和body之间要有空隔行

### 值中不需要引号!
@url = http://localhost:8080 
@name = wowotuo
@token =ad;ipiqedqeqeqeqepqei1213132;dkqdqeqe
@id =6
@user2 = {name :wukong,old: 32}

### =>"/";GET;
GET {{url}}/ HTTP/1.1

### =>"/html";GET
GET {{url}}/html HTTP/1.1
### /headers
GET {{url}}/headers  HTTP/1.1

### /path/:name/:id ; id为输入参数
GET {{url}}/path/{{name}}/{{id}} HTTP/1.1


### =>/form;有GET和POST两部分;POST方法要求输入
POST {{url}}/form HTTP/1.1
Content-Type: application/x-www-form-urlencoded

account={{name}}&password=dbfund



### /json; POST.输入user的json格式; 注意JSON格式要"双引号"!
POST {{url}}/json HTTP/1.1
content-type: application/json; charset=utf-8

{
    "name": "{{name}} 你好!",
    "old": 19

}

### /user/:name/:old; 另外一种表达形式
GET {{url}}/user/{{name}}/32  HTTP/1.1

### /params
GET {{url}}/params?tradedays=3&price=3.9&name=breaking

### /hashmap; POST,要求输入hashmap
GET {{url}}/hashmap?name={{name}}&old="19"  HTTP/1.1


### 对于curl命令的支持 类似 =>GET {{url}}/html
curl --request GET \
     --url {{url}}/html

###--header 'user-agent: vscode-restclient'


举报

相关推荐

0 条评论