Cannot isolate middleware when using wildcard patterns at the same path prefix #3685
Unanswered
ThomasVille
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Environment
Problem description
I'm trying to serve both REST API endpoints and Swagger UI documentation at the same path prefix (
/api
) but with different middleware requirements. However, actix-web's routing behavior with wildcard patterns makes it impossible to properly isolate middleware between these different types of content.Current limitation
There appears to be no way to achieve this configuration:
GET /api/items
- REST endpoint WITH middleware.GET /api/*
- Swagger UI WITHOUT middleware.My current solution
With the following code, the middleware is applied only to the REST endpoint as expected, but Swagger UI is served at a different path prefix, which is not what I want:
cargo run
.http://127.0.0.1:8080/docs/
: Nothing logged as expected.http://127.0.0.1:8080/api/items
: Logged as expected.Cargo.toml:
Attempted approaches
Approach 1: Different Path Prefixes (Works, but not desired)
Result: ✅ Middleware isolation works, but API is at
/api/items
and docs at/docs/
(different prefixes)Approach 2: Same Scope, SwaggerUI Last
Result: ❌ Cannot selectively apply middleware - it affects both REST endpoints AND Swagger UI
Approach 3: Separate Scopes, Same Prefix (Doesn't work)
Result: ❌ One of the scopes is ignored entirely.
Questions
Use Case
I want to serve a REST API with authentication middleware alongside Swagger UI documentation, both at
/api/*
. This is a common pattern for API servers where documentation is co-located with the API itself.Impact
This limitation forces developers to either:
Existing issues
Beta Was this translation helpful? Give feedback.
All reactions