Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions axum/src/docs/method_routing/route_layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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?

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 [`MethodRouter::layer`] except the middleware will only run if
the request matches a route. This is useful for middleware that return early
Expand All @@ -28,6 +31,6 @@ let app = Router::new().route(

// `GET /foo` with a valid token will receive `200 OK`
// `GET /foo` with a invalid token will receive `401 Unauthorized`
// `POST /FOO` with a invalid token will receive `405 Method Not Allowed`
// `POST /foo` with a invalid token will receive `405 Method Not Allowed`
# let _: Router = app;
```
12 changes: 8 additions & 4 deletions axum/src/docs/routing/route_layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 (default fallback handler) after the 404 Not Found?

# let _: Router = app;
```
Loading