您现在的位置是:首页 >其他 >【Rust学习】web框架 Axum,提供REST API网站首页其他

【Rust学习】web框架 Axum,提供REST API

吃饱了好睡觉耶1 2024-08-15 12:01:03
简介【Rust学习】web框架 Axum,提供REST API

cargo-watch:有修改就重启服务器,类似java web的热部署
安装:cargo install cargo-watch
使用:cargo watch -x run
这样每次有修改就会自动重启web服务

vscode插件Thunder Client(类似postman)
在这里插入图片描述

hello,world

建议用cargo add的方式添加

[dependencies]
axum = { version = "0.6.1", features = ["headers", "macros"] }
serde = { version = "1.0.147", features = ["derive"] }
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
tower-http = { version = "0.4.0", features = ["cors"] }
use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/hello", get(hello_world));

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap()
}

async fn hello_world() -> String {
    "Hello world!!!!!".to_owned()
}

在这里插入图片描述

routing

代码整理

不希望把逻辑都放在main
main.rs

use routing::run;

#[tokio::main]
async fn main() {
    run().await;
}

lib.rs

use axum::{routing::get, Router};

pub async  fn run(){
    let app = Router::new().route("/hello", get(hello_world));

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
    .serve(app.into_make_service())
    .await
    .unwrap()
}

async fn hello_world() -> String {
    "Hello world!!!!!".to_owned()
}

more separate
在这里插入图片描述
lib.rs

mod routes;
use routes::create_routes;

pub async  fn run(){
    let app = create_routes();

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
    .serve(app.into_make_service())
    .await
    .unwrap()
}


routes/mod.rs

use axum::{body::Body,routing::get, Router};
mod hello_world;
use hello_world::hello_world;
pub fn create_routes() -> Router<(), Body> {
    Router::new().route("/", get(hello_world))
}

routes/hello_world.rs

pub async fn hello_world() -> String {
    "Hello World from my own file".to_owned()
}

main.rs

use routing::run;

#[tokio::main]
async fn main() {
    run().await;
}

JSON

use axum::Json;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct MirrorJson {
    message: String,
}

#[derive(Serialize)]
pub struct MirrorJsonResponse {
    message: String,
    message_from_server: String,
}

pub async fn mirror_body_json(Json(body): Json<MirrorJson>) -> Json<MirrorJsonResponse> {
    Json(MirrorJsonResponse {
        message: body.message,
        message_from_server: "Hello from Axum".to_owned(),
    })
}
 

在这里插入图片描述

path variable

mod.rs

use axum::{
    body::Body,
    routing::{get,patch, post},
    Router,
};
mod hello_world;
use hello_world::hello_world;
mod mirror_body_string;
use mirror_body_string::mirror_body_string;
mod mirror_body_json;
use mirror_body_json::mirror_body_json;
mod path_variables;
use path_variables::{path_variables,hard_coded_path};
pub fn create_routes() -> Router<(), Body> {
    Router::new()
        .route("/", patch(hello_world))
        .route("/mirror_body_string", post(mirror_body_string))
        .route("/mirror_body_json", post(mirror_body_json))
        .route("/path_variables/15", get(hard_coded_path))//这个要先来,否则会被匹配到下一个去
        .route("/path_variables/:id", get(path_variables))
        

}

use axum::extract::Path;

pub async fn path_variables(Path(id): Path<i32>) -> String {
    id.to_string()
}

pub async fn hard_coded_path() -> String {
    "You got 15!".to_owned()
}
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。