Tower compat
Salvo provides compatibility support for the tower ecosystem through the tower-compat
function. For specific APIs, please see the documentation.
Example
use salvo::prelude::*;
use tokio::time::Duration;
use tower::limit::RateLimitLayer;
#[handler]
async fn hello() -> &'static str {
"Hello World"
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();
let limit = RateLimitLayer::new(5, Duration::from_secs(30)).compat();
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
let router = Router::new().hoop(limit).get(hello);
Server::new(acceptor).serve(router).await;
}
[package]
name = "example-with-tower"
version.workspace = true
edition.workspace = true
publish.workspace = true
[dependencies]
salvo = { workspace = true, features = ["tower-compat"] }
tokio = { workspace = true, features = ["macros"] }
tower = { version = "0.4", features = ["limit"] }
tracing.workspace = true
tracing-subscriber.workspace = true