Module middleware

Module middleware 

Source
Expand description

Middleware functionality for HTTP request and response processing.

This module provides the core infrastructure for implementing middleware in the HTTP processing pipeline. Middleware allows you to modify or inspect HTTP requests and responses during processing, enabling functionality like:

  • Request/response logging
  • Authentication and authorization
  • Request timeouts
  • Response compression
  • Custom headers
  • Request/response transformation

§Usage

Implement the Middleware trait to create custom middleware:

use http_kit::{Request, Response, Result, Endpoint, middleware::Middleware, Error};
use http_kit::middleware::MiddlewareError;

struct MyMiddleware;

impl Middleware for MyMiddleware {
    type Error = Error;
    async fn handle<E: Endpoint>(&mut self, request: &mut Request, mut next: E) -> Result<Response, MiddlewareError<E::Error, Self::Error>> {
        // Pre-processing
        let response = next.respond(request).await.map_err(MiddlewareError::Endpoint)?;
        // Post-processing
        Ok(response)
    }
}

The middleware can then be composed with endpoints using [WithMiddleware]. Multiple middleware can be chained together using tuples like (Middleware1, Middleware2).

Structs§

AnyMiddleware
Type-erased middleware that can hold any middleware implementation behind a trait object.

Enums§

MiddlewareError
Error type for middleware that can represent errors from either the middleware itself or the endpoint it wraps.
MiddlewareTupleError
Error type for middleware tuples, representing errors from either middleware.

Traits§

Middleware
Trait for implementing middleware that can process HTTP requests and responses.