Run middleware for requests with no matching route (#422)

While thinking about #419 I realized that `main` had a bug where
middleware wouldn't be called if no route matched the incoming request.
`Router` would just directly return a 404 without calling any
middleware.

This fixes by making the fallback default to a service that always
returns 404 and always calling that if no route matches.

Layers applied to the router is then also applied to the fallback.

Unfortunately this breaks #380 but I don't currently see a way to
support both. Auth middleware need to run _after_ routing because you
don't care about auth for unknown paths, but logging middleware need to
run _before_ routing because they do care about seeing requests for
unknown paths so they can log them...

Part of #419
This commit is contained in:
David Pedersen
2021-10-26 18:39:05 +02:00
committed by GitHub
parent e9533c566f
commit 8fe4eaf1d5
6 changed files with 225 additions and 110 deletions
-3
View File
@@ -41,9 +41,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **fixed:** Adding a conflicting route will now cause a panic instead of silently making
a route unreachable.
- **fixed:** Route matching is faster as number of routes increase.
- **fixed:** Middleware that return early (such as `tower_http::auth::RequireAuthorization`)
now no longer catch requests that would otherwise be 404s. They also work
correctly with `Router::merge` (previously called `or`) ([#408])
- **fixed:** Correctly handle trailing slashes in routes:
- If a route with a trailing slash exists and a request without a trailing
slash is received, axum will send a 301 redirection to the route with the