Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware:
    MaybeSend
    + MaybeSync
    + 'static {
    // Required method
    fn handle<'a>(
        &'a self,
        ctx: DispatchContext<'a>,
        next: Next<'a>,
    ) -> impl Future<Output = Result<HandlerAction, ProxyError>> + MaybeSend + 'a;

    // Provided method
    fn after_dispatch(
        &self,
        _completed: &CompletedRequest<'_>,
    ) -> impl Future<Output = ()> + MaybeSend + '_ { ... }
}
Expand description

Composable post-auth middleware for the dispatch chain.

Implement this trait to intercept requests after identity resolution and authorization but before (or instead of) backend dispatch. Each middleware receives the DispatchContext and a Next handle to continue the chain.

struct RateLimiter;

impl Middleware for RateLimiter {
    async fn handle<'a>(
        &'a self,
        ctx: DispatchContext<'a>,
        next: Next<'a>,
    ) -> Result<HandlerAction, ProxyError> {
        if self.is_over_limit(&ctx) {
            Ok(HandlerAction::Response(ProxyResult { status: 429, .. }))
        } else {
            next.run(ctx).await
        }
    }
}

Required Methods§

Source

fn handle<'a>( &'a self, ctx: DispatchContext<'a>, next: Next<'a>, ) -> impl Future<Output = Result<HandlerAction, ProxyError>> + MaybeSend + 'a

Handle a request, optionally delegating to the next middleware via Next::run.

Provided Methods§

Source

fn after_dispatch( &self, _completed: &CompletedRequest<'_>, ) -> impl Future<Output = ()> + MaybeSend + '_

Called after the request has been fully dispatched and the response is available. Use this for logging, metering, or other post-dispatch side effects. The default implementation is a no-op.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§