Nested routes with multiple service #742
-
Hello, I have an API like this :
I tried this approach : let rest_api = Router::new().route("/", get(rest_handler).post(rest_handler);
let ws_api = Router::new().route("/", get(ws_handler));
let routes = Router::new()
.nest("/a", rest_api)
.nest("/b", rest_api_clone)
.nest("/c", ws_api);
let app = Router::new().nest("/api/v1.0/area/", routes); It works but it's not very dynamic neither convenient as I have a dozen other REST routes, and I don't want to update this part for every routes. Can someone suggests me another way ? or maybe I'm missing something ? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
You either have to do what you're doing now or capture the path param (with |
Beta Was this translation helpful? Give feedback.
-
Yes thanks, I use match on Path but struggle to find a common type between I have something like : async fn matcher(Extension(state): Extension<Arc<State>>, req: TYPE_HERE?) -> impl IntoResponse {
// ...
match some_key {
ws_key => websocket_handler(state, req).await,
_ => handler(state, req).await
}
} Handlers : // rest
pub async fn handler(state: Arc<State>, mut req: Request<Body>) -> impl IntoResponse
// ws
pub async fn websocket_handler(state: Arc<State>, ws: WebSocketUpgrade) -> impl IntoResponse |
Beta Was this translation helpful? Give feedback.
You either have to do what you're doing now or capture the path param (with
/*foo
) and match onfoo
and call the respective handler.