Skip to main content

IMiddleware

Trait IMiddleware 

Source
pub trait IMiddleware: Send + Sync {
    // Required method
    fn invoke<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 mut dyn IHttpContext,
    ) -> Pin<Box<dyn Future<Output = Result<ControlFlow<()>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided method
    fn after<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 mut dyn IHttpContext,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Middleware component in the HTTP request pipeline.

Middlewares are called in registration order. Each middleware can:

  • Inspect/modify the request before passing through
  • Short-circuit by returning ControlFlow::Break(())
  • The pipeline continues to the next middleware on ControlFlow::Continue(())

On short-circuit, remaining middleware and the final handler are skipped, but after hooks on already-executed middleware still run in reverse order.

Analogous to ASP.NET Core’s IMiddleware.

Required Methods§

Source

fn invoke<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 mut dyn IHttpContext, ) -> Pin<Box<dyn Future<Output = Result<ControlFlow<()>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Process an HTTP request (before hook).

Return Ok(ControlFlow::Continue(())) to continue the pipeline, or Ok(ControlFlow::Break(())) to short-circuit (skip remaining middleware and the final handler; after hooks on already-executed middleware still run in reverse order).

Provided Methods§

Source

fn after<'life0, 'life1, 'async_trait>( &'life0 self, _ctx: &'life1 mut dyn IHttpContext, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called after the final handler has executed.

Middleware post-processing, logging, response modification. Default: no-op.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§