Expand description
Middleware: tower layers over the whole application, plus batteries.
App::layer accepts any tower::Layer over BoxService, the
application as a Service<http::Request<Body>>. from_fn builds one
from an async function.
§Ordering
The first layer registered is the outermost: it sees the request first
and the response last, exactly like reading ServiceBuilder top to bottom.
app.request_id().request_logging().cors(..) runs request-id, then
logging, then CORS, then the routes. Registration order relative to
with_state, provide, … does not matter: middleware always wraps
those. Middleware of the root app also wraps 404s and the documentation
endpoints; middleware of a mounted child wraps only that child’s routes.
§Errors
Rejections produced by built-in middleware are RFC 7807
application/problem+json documents (ApiError).
§Built-ins
Cors, Compression, CsrfOrigin, TrustedHosts,
HttpsRedirect, RequestIdLayer (+ RequestId extractor),
RequestLogging, Timeout, ConcurrencyLimit, BodyLimit and
RateLimit, each with a same-named convenience method on App.
Structs§
- Body
Limit - Rejects request bodies larger than a limit with a 413 problem.
- Compression
- Response compression (gzip and brotli, negotiated via
Accept-Encoding). - Concurrency
Limit - Bounds execution and waiting, returning 503 with Retry-After on overload.
- Concurrency
Limit Error - Invalid concurrency or queue-deadline configuration.
- Concurrency
Stats - Admission counters shared by all clones of a limit.
- Cors
- Cross-origin resource sharing.
- Csrf
Origin - Rejects cross-site state-changing requests by checking their origin.
- Csrf
Origin Error - A configured origin that is not
scheme://host[:port]. - From
FnLayer - Layer returned by
from_fn. - Https
Redirect - Redirect plain HTTP to HTTPS with an explicit proxy and authority policy.
- Next
- The rest of the middleware chain, handed to
from_fnclosures. - Proxy
Context - Validated client and transport context from one proxy policy.
- Proxy
Error - Invalid or ambiguous headers supplied by a trusted proxy peer.
- Rate
Limit - Token-bucket rate limiter keyed by validated client IP.
- Rate
Limit Error - Invalid rate-limiter configuration.
- Request
Id - The id of the current request, set by the
RequestIdLayermiddleware. - Request
IdLayer - Assigns every request an id: an incoming
x-request-idis propagated when it is 1..=128 visible ASCII characters, otherwise a UUID v4 is generated. The id is exposed asRequestId, forwarded to the handler as a request header and echoed on the response. - Request
Logging - Emits an
http.requesttracing span per request withrequest_id,method,route(matched path template via axumMatchedPath, or"<unmatched>"),statusandlatency_ms. - Timeout
- Fails requests that take longer than a deadline (default status 504).
- Trusted
Hosts - Rejects requests whose
Hostis not on an allow-list (400 problem). - Trusted
Proxies - Proxy peers permitted to supply forwarded client and scheme headers.
Functions§
- from_fn
- Build a middleware layer from an async function
(Request, Next) -> Response.
Type Aliases§
- BoxService
- The service type middleware layers wrap: the rest of the application.