pub trait Middleware: Send + Sync {
type Error: IntoResponse;
// Required method
fn call(
&self,
request: Request,
next: impl Next,
) -> impl Future<Output = Result<Response, Self::Error>> + Send;
}Expand description
Middleware that wraps request handling on an HTTP server.
A middleware receives each request together with a Next continuation. It
may inspect or modify the request, short-circuit with its own response, or
call next and then inspect or modify the response.
Register it as a concrete instance with AddMiddlewareExt::add_middleware,
or resolved from the DI container with
AddMiddlewareServiceExt::add_middleware_service (the latter additionally
requires the type to be a Service, so it can hold injected dependencies).
Attach it to routes with the #[router(middleware = [..])] /
#[route(middleware = [..])] macro attributes.
§Ordering
Within a middleware = [A, B] list the first entry is outermost: it runs
first on the request and last on the response. Router-level middleware wraps
route-level middleware. So #[router(middleware = [A, B])] combined with
#[route(middleware = [C, D])] enters as A, B, C, D and unwinds as
D, C, B, A.
Required Associated Types§
Sourcetype Error: IntoResponse
type Error: IntoResponse
Error type rendered into a response when call
returns Err.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".