I'm building an HTTP api with hyper
and I'm having trouble with dynamic routing.
In the beginning I only had static routes, my routing was done by a simple match:
async fn route(req: hyper::Request<hyper::Body>) -> hyper::Response<hyper::Body> {
match (req.method(), req.uri().path) {
(&hyper::Method::GET, "/version") => unimplemented!(),
// etc
}
}
This worked great until I needed to add dynamic routes (e.g /post/1234
). I found the httprouter
crate but it requires route handlers to be Sync
. Most of my route handlers are !Sync
since I use reqwest
and async_trait
all of which returns !Sync
futures.
I dived into httprouter
source code and found that route handlers must be Sync
because otherwise you can't wrap a Router
(which owns the handlers) in an Arc
to use it across connections.
The easy fix would be to create a clone of the router for each connection but this seems wildly inefficient.
How would you do it? Is it even possible with !Sync
futures?