-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Allow route fallback #257
Comments
I think allowing extractors to reject and request and forward to the next route is a good idea. Its very similar to rocket's request guards. I haven't tested it yet but I think that should be possible to implement. However I don't it is possible for
I haven't thought deeply about if there is a way around it. |
Sounds to me like 404 handlers need to be special-cased afterall, then there is no expectation that they will receive the original request 😉 Maybe my 404 handler will just have to be defined like the one in the previous 404 example ( A related problem: When extractors are allowed to forward a request on failure, the next handler might fail because it tries to take the request body when that was already taken from the first handler. I'd assume extractors are applied in-order, so should the docs encourage putting body extractors last? |
Yep that would certainly be an issue. I know warp has the same issue where filters can reject requests in this way. Warp returns a 500 if you attempt to consume the request body twice. So yeah we'd have to document that. |
I just thought of something. If extractors failing will automatically fallback and try the next route it would cause issues with Router::new()
// route that accepts all requests
.nest("/", _)
// `Auth ` is an extractor that authorizes requests
.route("/", get(|_: Auth| async { }); This would cause unauthorized requests to fallback to the nested route, which probably isn't what you'd want. Assuming we have wildcard routes like |
Yeah, that's why I only proposed the path extractor changing its behavior. I don't think falling back on something like an invalid JSON payload, missing auth header or non-UTF8 query parameters makes any sense. |
Yeah thats true. I wonder if there is a way for extractors to explicitly opt-in to fallback/forwarding. Maybe by returning a special error type 🤔 |
Yeah, that's roughly what I was thinking. Should probably expose it too so user-defined extractors can opt into the same behavior. |
With #408 we now have While that doesn't allow for extractors to cause fallback I believe this is enough to close this issue. I do think fallback extractors is an interesting idea but we also have to consider if it could lead to overly complex routers. Also not quite sure how it would work with the new matchit router. Realistically not something axum will support soon but feel free to open a new issue specifically about that to discuss it. |
Feature Request
I'd like to be able to use
.or()
or something similar to fall back to another route when an extractor (in particular a path extractor) fails, or atower_http::services::ServeDir
instance didn't find a matching file. Currently, the handler supplied to.or()
is only used when a route didn't match by the first parameter to.route()
, and when adding multiple.or()
s onto each other only one is ever used (I think).Motivation
https://turbo.fish has its static assets right next to dynamic routes. I think this is not a bad design: Since the dynamic routes only ever look like
::<_>
or<_>::
(underscore being a wildcard here), there is never an ambiguity with static files in practice and nesting static files under something like/static/
would prevent me from addingfavicon.ico
to the root in a straight-forward manner, which is a best practice. Also I like the shorter paths when there is no ambiguity anyways.Proposal
Have
.or()
call the given service not only if no other route matched, but also if a route's path extractor failed, or an inner service liketower_http::services::ServeDir
returns some sort of not-found rejection.Alternatives
🤷
The text was updated successfully, but these errors were encountered: