From b11906d790d0180a80e0da9789ab37b749903336 Mon Sep 17 00:00:00 2001 From: Omer Zuarets Date: Wed, 30 Jul 2025 14:06:00 +0300 Subject: [PATCH] Update documentation for route layer usage --- axum/src/docs/method_routing/route_layer.md | 11 +++++++---- axum/src/docs/routing/route_layer.md | 12 ++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/axum/src/docs/method_routing/route_layer.md b/axum/src/docs/method_routing/route_layer.md index 501b55174d..4fc86ec24f 100644 --- a/axum/src/docs/method_routing/route_layer.md +++ b/axum/src/docs/method_routing/route_layer.md @@ -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 [`MethodRouter::layer`] except the middleware will only run if the request matches a route. This is useful for middleware that return early @@ -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; ``` diff --git a/axum/src/docs/routing/route_layer.md b/axum/src/docs/routing/route_layer.md index 9cce3ea79e..d9ae3bc8eb 100644 --- a/axum/src/docs/routing/route_layer.md +++ b/axum/src/docs/routing/route_layer.md @@ -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) # let _: Router = app; ```