-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor route matching to occur after middleware #36
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -88,54 +88,59 @@ async fn handle_request( | |||||||||||||||||||||||||||||||||
| // Convert hyper request to our Request type first | ||||||||||||||||||||||||||||||||||
| let (parts, body) = req.into_parts(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Match the route to get path params | ||||||||||||||||||||||||||||||||||
| let (handler, params) = match router.match_route(&path, &method) { | ||||||||||||||||||||||||||||||||||
| RouteMatch::Found { handler, params } => (handler.clone(), params), | ||||||||||||||||||||||||||||||||||
| RouteMatch::NotFound => { | ||||||||||||||||||||||||||||||||||
| let response = ApiError::not_found(format!("No route found for {} {}", method, path)) | ||||||||||||||||||||||||||||||||||
| .into_response(); | ||||||||||||||||||||||||||||||||||
| log_request(&method, &path, response.status(), start); | ||||||||||||||||||||||||||||||||||
| return response; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| RouteMatch::MethodNotAllowed { allowed } => { | ||||||||||||||||||||||||||||||||||
| let allowed_str: Vec<&str> = allowed.iter().map(|m| m.as_str()).collect(); | ||||||||||||||||||||||||||||||||||
| let mut response = ApiError::new( | ||||||||||||||||||||||||||||||||||
| StatusCode::METHOD_NOT_ALLOWED, | ||||||||||||||||||||||||||||||||||
| "method_not_allowed", | ||||||||||||||||||||||||||||||||||
| format!("Method {} not allowed for {}", method, path), | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| .into_response(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| response | ||||||||||||||||||||||||||||||||||
| .headers_mut() | ||||||||||||||||||||||||||||||||||
| .insert(header::ALLOW, allowed_str.join(", ").parse().unwrap()); | ||||||||||||||||||||||||||||||||||
| log_request(&method, &path, response.status(), start); | ||||||||||||||||||||||||||||||||||
| return response; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Build Request (initially streaming) | ||||||||||||||||||||||||||||||||||
| // Build Request with empty path params (will be set after route matching) | ||||||||||||||||||||||||||||||||||
| let request = Request::new( | ||||||||||||||||||||||||||||||||||
| parts, | ||||||||||||||||||||||||||||||||||
| crate::request::BodyVariant::Streaming(body), | ||||||||||||||||||||||||||||||||||
| router.state_ref(), | ||||||||||||||||||||||||||||||||||
| params, | ||||||||||||||||||||||||||||||||||
| crate::path_params::PathParams::new(), | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Apply request interceptors (in registration order) | ||||||||||||||||||||||||||||||||||
| let request = interceptors.intercept_request(request); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Create the final handler as a BoxedNext | ||||||||||||||||||||||||||||||||||
| let final_handler: BoxedNext = Arc::new(move |req: Request| { | ||||||||||||||||||||||||||||||||||
| let handler = handler.clone(); | ||||||||||||||||||||||||||||||||||
| Box::pin(async move { handler(req).await }) | ||||||||||||||||||||||||||||||||||
| // Create the routing handler that does route matching inside the middleware chain | ||||||||||||||||||||||||||||||||||
| // This allows CORS and other middleware to intercept requests BEFORE route matching | ||||||||||||||||||||||||||||||||||
| let router_clone = router.clone(); | ||||||||||||||||||||||||||||||||||
| let path_clone = path.clone(); | ||||||||||||||||||||||||||||||||||
| let method_clone = method.clone(); | ||||||||||||||||||||||||||||||||||
| let routing_handler: BoxedNext = Arc::new(move |mut req: Request| { | ||||||||||||||||||||||||||||||||||
| let router = router_clone.clone(); | ||||||||||||||||||||||||||||||||||
| let path = path_clone.clone(); | ||||||||||||||||||||||||||||||||||
| let method = method_clone.clone(); | ||||||||||||||||||||||||||||||||||
| Box::pin(async move { | ||||||||||||||||||||||||||||||||||
| match router.match_route(&path, &method) { | ||||||||||||||||||||||||||||||||||
| RouteMatch::Found { handler, params } => { | ||||||||||||||||||||||||||||||||||
| // Set path params on the request | ||||||||||||||||||||||||||||||||||
| req.set_path_params(params); | ||||||||||||||||||||||||||||||||||
| handler(req).await | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| RouteMatch::NotFound => { | ||||||||||||||||||||||||||||||||||
| ApiError::not_found(format!("No route found for {} {}", method, path)) | ||||||||||||||||||||||||||||||||||
| .into_response() | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| RouteMatch::MethodNotAllowed { allowed } => { | ||||||||||||||||||||||||||||||||||
| let allowed_str: Vec<&str> = allowed.iter().map(|m| m.as_str()).collect(); | ||||||||||||||||||||||||||||||||||
| let mut response = ApiError::new( | ||||||||||||||||||||||||||||||||||
| StatusCode::METHOD_NOT_ALLOWED, | ||||||||||||||||||||||||||||||||||
| "method_not_allowed", | ||||||||||||||||||||||||||||||||||
| format!("Method {} not allowed for {}", method, path), | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| .into_response(); | ||||||||||||||||||||||||||||||||||
| response | ||||||||||||||||||||||||||||||||||
| .headers_mut() | ||||||||||||||||||||||||||||||||||
| .insert(header::ALLOW, allowed_str.join(", ").parse().unwrap()); | ||||||||||||||||||||||||||||||||||
| response | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+130
to
+133
|
||||||||||||||||||||||||||||||||||
| response | |
| .headers_mut() | |
| .insert(header::ALLOW, allowed_str.join(", ").parse().unwrap()); | |
| response | |
| let allowed_header_value = allowed_str.join(", "); | |
| if let Ok(header_value) = allowed_header_value.parse() { | |
| response | |
| .headers_mut() | |
| .insert(header::ALLOW, header_value); | |
| } else { | |
| error!( | |
| allowed_methods = %allowed_header_value, | |
| "Failed to parse Allow header value; skipping header insertion" | |
| ); | |
| } | |
| response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Multiple clones are performed on every request inside the closure. The
router_clone.clone()on line 108 is cloning anArc<Router>which is inexpensive, butpath_clone.clone()andmethod_clone.clone()create additional allocations for each request. Consider moving these into captured variables in the outer scope to avoid repeated cloning per request.