-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Update documentation for route layer usage #3422
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
Open
omer9564
wants to merge
1
commit into
tokio-rs:main
Choose a base branch
from
omer9564:docs/issue-3421
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,12 @@ Apply a [`tower::Layer`] to the router that will only run if the request matches | |
| a route. | ||
|
|
||
| Note that the middleware is only applied to existing routes. So you have to | ||
| first add your routes (and / or fallback) and then call `route_layer` | ||
| afterwards. Additional routes added after `route_layer` is called will not have | ||
| the middleware added. | ||
| first add your routes and then call `route_layer` afterwards. Additional routes | ||
| added after `route_layer` is called will not have the middleware added. | ||
|
|
||
| **Important**: The middleware is NOT applied to fallback handlers. Fallback handlers | ||
| will run without the middleware applied. This is because `route_layer` only applies | ||
| to matched requests, while fallbacks handle unmatched requests. | ||
|
|
||
| This works similarly to [`Router::layer`] except the middleware will only run if | ||
| the request matches a route. This is useful for middleware that return early | ||
|
|
@@ -26,10 +29,11 @@ use tower_http::validate_request::ValidateRequestHeaderLayer; | |
|
|
||
| let app = Router::new() | ||
| .route("/foo", get(|| async {})) | ||
| .fallback(|| async { "fallback" }) | ||
| .route_layer(ValidateRequestHeaderLayer::bearer("password")); | ||
|
|
||
| // `GET /foo` with a valid token will receive `200 OK` | ||
| // `GET /foo` with a invalid token will receive `401 Unauthorized` | ||
| // `GET /not-found` with a invalid token will receive `404 Not Found` | ||
| // `GET /not-found` with a invalid token will receive `200 OK` (fallback) | ||
|
Comment on lines
+32
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this example benefits from having an explicit fallback handler. Why did you add that? Maybe just add |
||
| # let _: Router = app; | ||
| ``` | ||
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.
This paragraph is redundant with the one after it. Maybe that one isn't quite clear, but I don't think leaving it as-is and adding an extra one before it is the right fix. Can you think about how to merge these two paragraphs?