马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Axum作为Rust当红Web框架,值得相识一下。下面实例包罗几个范例的常场场景。
具体如下:
一、Axum
1、toml中依赖- [dependencies]
- tokio = {version="1.39.3",features =["full"]}
- axum ={version ="0.7.5",features =["tokio"]}
- 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;
- use std::sync::Arc;
- #[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>,//Option=>字段可以缺省
- price:Option<f64>,
- name: Option<String>,
- }
- #[derive(Deserialize,Debug)]
- struct Strategy {
- name: String,
- amount: f64,
- id: i32,
- }
- #[derive(Debug)]
- struct AppState {
- count: i32
- }
- #[tokio::main]
- async fn main() {
- let shared_state = Arc::new(AppState { count:0 });
- 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("/query_json",post(
- {
- let shared_state = Arc::clone(&shared_state);
- move |body| query_json(body,shared_state)
- }
- ,))
- .route("/params",get(get_params))
- .route("/strategies/", get(query_strategy));
- 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)
- }
- //多个复杂的参数
- async fn query_strategy(Json(strategy): Json<Strategy>) ->String{
- println!("strategy :{:?}",strategy);
- format!("receive=> strategy:{:?}",strategy)
- }
- //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 }))
- }
- async fn query_json(Json(user):Json<User>,arc: Arc<AppState>)->String{
- println!("arc:{:?}",arc);
- format!("user:{:?} ",user)
- }
- // `&'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工具协同测试
在vscode中下载Rest Client插件后,通过创建以.http 或 .rest为末端的文件,举行http哀求测试。具体Rest Client功能很强盛,这里只先容,与Axum共同测试的方案。
具体如下:- # 头文件和body之间要有空隔行
- ### 值中不需要引号!
- @url = http://localhost:8080
- @name = wowotuo
- @token =ad;ipiqedqeqeqeqepqei1213132;dkqdqeqe
- @id =6
- ### =>"/";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'
复制代码 三、输出
用Rest Client调试的效果好坏常直观。
|