Skip to main content

MiddlewareService

Trait MiddlewareService 

Source
pub trait MiddlewareService: Service<Handle = Arc<Self>> {
    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.

Middleware is a Service, so it is built by the container and may hold injected dependencies. Attach it to routes with the #[router(middleware = [..])] / #[route(middleware = [..])] macro attributes, and register the service with AddMiddlewareExt::add_middleware.

§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§

Source

type Error: IntoResponse

Error type rendered into a response when call returns Err.

Required Methods§

Source

fn call( &self, request: Request, next: impl Next, ) -> impl Future<Output = Result<Response, Self::Error>> + Send

Handles request, optionally delegating to next.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§