-
Notifications
You must be signed in to change notification settings - Fork 1.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
Change Router::nest
to flatten the routes
#1711
Conversation
axum/src/routing/mod.rs
Outdated
debug_assert!(path.starts_with('/')); | ||
|
||
if prefix.ends_with('/') { | ||
format!("{prefix}{}", path.trim_start_matches('/')).into() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I'm wondering how multiple successive slashes behave elsewhere 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like if you do .nest("////", _)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that or .nest("///foo/", _)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've nerd sniped me 😜 I'll make a note and do some tests.
@jplatte I finally got fallback inheritance working properly 🎉 |
@@ -262,6 +282,23 @@ mod tests { | |||
assert_eq!(res.status(), StatusCode::OK); | |||
} | |||
|
|||
#[tokio::test] | |||
async fn can_extract_nested_matched_path_in_middleware_via_extension_using_nest() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌🏼
Co-authored-by: Jonas Platte <[email protected]>
@@ -29,6 +29,7 @@ where | |||
} | |||
|
|||
fn call(&mut self, _req: Request<B>) -> Self::Future { | |||
println!("NotFound hit"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this stay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not 😅 removed in v0.6.14
With the introduction of `PathRouter` in #1711 I forgot that fallbacks shouldn't be able to extract `MatchedPath`. The extension for it was interested regardless if the `PathRouter` was used as a fallback or not. It doesn't make sense to extract `MatchedPath` in a fallback because there was no matched route. Turns out it also fixes a panic with a specifical fallback/nest setup. Fixes #1931
With the introduction of `PathRouter` in #1711 I forgot that fallbacks shouldn't be able to extract `MatchedPath`. The extension for it was interested regardless if the `PathRouter` was used as a fallback or not. It doesn't make sense to extract `MatchedPath` in a fallback because there was no matched route. Turns out it also fixes a panic with a specifical fallback/nest setup. Fixes #1931
Thanks for this! I think this is responsible for fixing an issue we had where the telemetry we had for nested routers didn't get their |
Yep that most likely #1441 |
This changes
Router::nest
to "flatten" the routes. By flattening I mean justmoving the routes from one router to another by calling
.route(nest_path, nested_endpoint)
. That means there aren't anyRouter
snested inside each other and instead we just have one flattened
Router
whichperforms better.
The main tricky part was fallback inheritance. The main reason we didn't flatten
routes in 0.6 previously was specifically because of fallback inheritance.
The solution I went for here was to make an internal
PathRouter
type. It hasmost routing functions from
Router
, except fallbacks. ARouter
thencontains two
PathRouter
s: One for the actual routes and another forfallbacks. Then when you add a fallback to the root router that adds
/
and/*
routes to the fallbackPathRouter
. Nesting is then pretty straightforward: We just nest the routes from each
PathRouter
.Fixes #1624
Fixes #1441
TODO
MatchedPath
Don't believe there are any docs changes to make.