Add support for tower compat
prabirshrestha opened this issue · comments
Add support for tower compatibility later so that we can use reuse middleware for tower.
An example would be https://github.com/Totodore/socketioxide which is large that may not make sense to easily port it to trillium.
Salvo seems to have .compat()
method allowing to easily convert a tower compliance middleware to salvo. https://github.com/salvo-rs/salvo/blob/main/examples/with-tower/src/main.rs. Notice the RateLimitLayer
middleware.
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;
}
Having something like tower()
compat similar to api()
would be great.
use tower::limit::RateLimitLayer;
fn main() {
env_logger::init();
trillium_smol::run((
logger(),
router()
.post("/", tower(RateLimitLayer::new(5, Duration::from_secs(30))))
.get("/", api(get_post))
.put("/", api(save_post_alternative)),
));
}
Thanks for the suggestion, but this is not something I intend to support. I do intend to add a trillium rate limiting crate at some point, though, and socket io is something I'd like to add as well
It may be possible for someone else to build this using the http-compat-1 feature on trillium-http, but I'd much sooner implement trillium equivalents of tower components than maintain a shim between the abstractions