【Axum】定义多个路由
环境
- Time 2022-01-16
- Rust 1.58.0
- Axum 0.4.4
概念
参考:https://docs.rs/axum/latest/axum/index.html
示例
main.rs
use axum::{response::Html, routing::get, Router};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(home))
.route("/hello", get(|| async { "hello" }))
.route("/world", get(|| async { "world" }));
let addr = SocketAddr::from(([127, 0, 0, 1], 4444));
println!("listening on {addr}");
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn home() -> Html<&'static str> {
Html("hello world
")
}
总结
使用 axum 实现多个路由。