New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding nested fallbacks is harder than it should be #1053
Comments
In the short term, I believe that simply treating the nested router as an opaque service in the case where it has a fallback would work. You cannot currently add a nested router that has a fallback, so that approach can't possibly break any code. This would only need to be done if the nested router actually has a fallback, otherwise the current behavior is fine. |
That's a good idea actually 🤔 might get us most of the way there. |
I've thought about this some more and ran into this: let api = Router::new()
.fallback(api_fallback);
let app = Router::new()
.nest("/api", api)
.fallback(general_fallback)
.route_layer(some_layer); Middleware added with I think thats very surprising so unless we can find a solution for that I don't think we should do it. The solution I have in mind is once ibraheemdev/matchit#18 is fixed then we change fallbacks to be regular wildcard routes. It would require tracking which routes were added with Then nesting routers will fallbacks should just work since the nested fallback would become |
I don't know what the expected time frame for ibraheemdev/matchit#18 is. If it will be a while, another potential workaround would be to detect the nested router w/fallback, don't merge it, but do convert all of its own routes to be wrapped in If I correctly understand the way this works, that would have a similar effect to merging the nested router with ibraheemdev/matchit#18 fixed, and since it's pushing the |
This is blocked by #1086 |
I've decided not to support this its not worth the additional complexity it adds. See #1086 (comment). |
Suggested solution (extended for clarity) so people don't have to click around: fn router() -> Router {
Router::new().fallback(...)
}
fn make_some_routes() -> Router {
router().route("/", ...)
}
fn make_more_routes() -> Router {
router().route("/", ...)
}
fn my_routes() -> Router {
router()
.nest("/a", make_some_routes())
.nest("/b", make_more_routes())
} |
If this is really the solution we're going with then please make sure any documentation recommending this explicitly recommends using |
We currently don't support nesting routers that have fallbacks. This panics:
This is because while
Router::nest
takesT: Service
it attempts to downcast that into aRouter
. If that succeeds it takes a different code path that simply moves routes from one router to another while adding the prefix. However that means we cannot have twofallbacks
since we only have oneRouter
in the end.A possible workaround is to use another
Router
as a fallback:While this works its not very obvious and it would be great if multiple nested fallbacks just worked.
Haven't thought through how to make it work but I believe it requires ibraheemdev/matchit#18
The text was updated successfully, but these errors were encountered: