Module interceptor

Module interceptor 

Source
Expand description

Request/Response Interceptor System for RustAPI

This module provides interceptors that can modify requests before handlers and responses after handlers, without the complexity of Tower layers.

§Overview

Interceptors provide a simpler alternative to middleware for common use cases:

  • Adding headers to all requests/responses
  • Logging and metrics
  • Request/response transformation

§Execution Order

Request interceptors execute in registration order (1 → 2 → 3 → Handler). Response interceptors execute in reverse order (Handler → 3 → 2 → 1).

§Example

use rustapi_core::{RustApi, interceptor::{RequestInterceptor, ResponseInterceptor}};

struct AddRequestId;

impl RequestInterceptor for AddRequestId {
    fn intercept(&self, mut req: Request) -> Request {
        req.extensions_mut().insert(uuid::Uuid::new_v4());
        req
    }
}

struct AddServerHeader;

impl ResponseInterceptor for AddServerHeader {
    fn intercept(&self, mut res: Response) -> Response {
        res.headers_mut().insert("X-Server", "RustAPI".parse().unwrap());
        res
    }
}

RustApi::new()
    .request_interceptor(AddRequestId)
    .response_interceptor(AddServerHeader)
    .route("/", get(handler))
    .run("127.0.0.1:8080")
    .await

Structs§

InterceptorChain
Chain of request and response interceptors.

Traits§

RequestInterceptor
Trait for intercepting and modifying requests before they reach handlers.
ResponseInterceptor
Trait for intercepting and modifying responses after handlers complete.