A powerful web framework built with a simplified design.
See the codeCreate a new project:
cargo new hello-salvo
cd hello-salvo
cargo add salvo tokio --features salvo/oapi,tokio/macros
Write your first app in src/main.rs:
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;
}
Run it:
cargo run
No complex traits or generics. Middleware is just a handler:
#[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)
Apply middleware to specific route branches:
Router::new()
// Public routes
.push(Router::with_path("articles").get(list_articles))
// Protected routes
.push(Router::with_path("articles").hoop(auth_check).post(create_article).delete(delete_article))
Salvo handlers can deserialize request bodies and return typed JSON responses:
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<Json<Todo>, ParseError> {
let payload = req.parse_json::<CreateTodo>().await?;
Ok(Json(Todo {
id: 1,
text: payload.text,
}))
}
For this pattern, add Serde's derive feature:
cargo add serde --features derive
Just change #[handler] to #[endpoint]:
#[endpoint]
async fn hello() -> &'static str {
"Hello World"
}
Get TLS certificates automatically from Let's Encrypt:
let listener = TcpListener::new("0.0.0.0:443")
.acme()
.add_domain("example.com")
.http01_challenge(&mut router)
.quinn("0.0.0.0:443"); // HTTP/3 support
cargo install salvo-cli
salvo new my_project
Check out the full list of community projects in our ECOSYSTEM.md.
Salvo consistently ranks among the fastest Rust web frameworks:
If you find Salvo useful, consider buying me a coffee.
Licensed under Apache License 2.0.
Overall
Maintained
Code review
crates.io
Latest release
Releases this year
(top 24 of 48)
1,090 followers · starred Sep 2024
689 followers · starred Nov 2022
801 followers · starred Jul 2022
163 followers · starred Mar 2023
A powerful web framework built with a simplified design.
See the codeCreate a new project:
cargo new hello-salvo
cd hello-salvo
cargo add salvo tokio --features salvo/oapi,tokio/macros
Write your first app in src/main.rs:
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;
}
Run it:
cargo run
No complex traits or generics. Middleware is just a handler:
#[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)
Apply middleware to specific route branches:
Router::new()
// Public routes
.push(Router::with_path("articles").get(list_articles))
// Protected routes
.push(Router::with_path("articles").hoop(auth_check).post(create_article).delete(delete_article))
Salvo handlers can deserialize request bodies and return typed JSON responses:
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<Json<Todo>, ParseError> {
let payload = req.parse_json::<CreateTodo>().await?;
Ok(Json(Todo {
id: 1,
text: payload.text,
}))
}
For this pattern, add Serde's derive feature:
cargo add serde --features derive
Just change #[handler] to #[endpoint]:
#[endpoint]
async fn hello() -> &'static str {
"Hello World"
}
Get TLS certificates automatically from Let's Encrypt:
let listener = TcpListener::new("0.0.0.0:443")
.acme()
.add_domain("example.com")
.http01_challenge(&mut router)
.quinn("0.0.0.0:443"); // HTTP/3 support
cargo install salvo-cli
salvo new my_project
Check out the full list of community projects in our ECOSYSTEM.md.
Salvo consistently ranks among the fastest Rust web frameworks:
If you find Salvo useful, consider buying me a coffee.
Licensed under Apache License 2.0.
Overall
Maintained
Code review
crates.io
Latest release
Releases this year
(top 24 of 48)
1,090 followers · starred Sep 2024
689 followers · starred Nov 2022
801 followers · starred Jul 2022
163 followers · starred Mar 2023