# salvo **Repository Path**: limoncc/salvo ## Basic Information - **Project Name**: salvo - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-11 - **Last Updated**: 2026-08-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
> 加入社区: 微信(chrislearn) | QQ群: 823441777 ## 特性 - **简单强大** - 零样板代码,会写函数就会写 Handler - **HTTP/1、HTTP/2、HTTP/3** - 开箱即用的全协议支持 - **灵活路由** - 树形路由,中间件可挂载到任意层级 - **自动证书** - 内置 ACME,自动获取和续期 TLS 证书 - **OpenAPI** - 一流的 OpenAPI 支持,自动生成文档 - **WebSocket & WebTransport** - 内置实时通信支持 - **基于 Hyper & Tokio** - 生产级异步运行时 ## 快速开始 创建项目: ```bash cargo new hello-salvo cd hello-salvo cargo add salvo tokio --features salvo/oapi,tokio/macros ``` 编写 `src/main.rs`: ```rust use salvo::prelude::*; #[handler] async fn hello() -> &'static str { "Hello World" } #[tokio::main] async fn main() { let router = Router::new().get(hello); let acceptor = TcpListener::new("127.0.0.1:7878").bind().await; Server::new(acceptor).serve(router).await; } ``` 运行: ```bash cargo run ``` ## 为什么选择 Salvo? ### 中间件 = Handler 无需复杂的泛型和 trait,中间件就是普通函数: ```rust #[handler] async fn add_header(res: &mut Response) { res.headers_mut().insert(header::SERVER, HeaderValue::from_static("Salvo")); } Router::new().hoop(add_header).get(hello) ``` ### 树形路由 + 中间件 为不同路由分支应用不同的中间件: ```rust Router::new() // 公开路由 .push(Router::with_path("articles").get(list_articles)) // 需要认证的路由 .push(Router::with_path("articles").hoop(auth_check).post(create_article).delete(delete_article)) ``` ### JSON API Handler 可以反序列化请求体,并返回类型化的 JSON 响应: ```rust use salvo::http::ParseError; use salvo::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Deserialize)] struct CreateTodo { text: String, } #[derive(Serialize)] struct Todo { id: u64, text: String, } #[handler] async fn create_todo(req: &mut Request) -> Result