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};

struct MyMiddleware;

impl Middleware for MyMiddleware {
    async fn handle(&self, request: &mut Request, next: impl Endpoint) -> Result<Response> {
        // Pre-processing
        let response = next.respond(request).await?;
        // 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.

Traits§

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