Compatibilidad con torre
Salvo brinda soporte de compatibilidad para el ecosistema de torres a través de la función tower-compat
. Para API específicas, [consulte la documentación] (https://docs.rs/salvo_extra/latest/salvo_extra/tower_compat/index.html).
Código de muestra
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