Skip to main content

Module middleware

Module middleware 

Source
Expand description

Copyright © 2025-2026 Wenze Wei. All Rights Reserved.

This file is part of Ri. The Ri project belongs to the Dunimd Team.

Licensed under the Apache License, Version 2.0 (the “License”); You may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

§Middleware Module

This module provides a flexible middleware system for the Ri gateway, allowing for request processing and modification through a chain of middleware components.

§Key Components

  • RiMiddleware: Trait defining the middleware interface
  • RiMiddlewareChain: Manages a chain of middleware components
  • Built-in Middleware: Auth, CORS, Logging, Request ID, and Rate Limiting implementations

§Design Principles

  1. Async Trait: Uses async_trait for async middleware execution
  2. Flexible Chain: Allows dynamic addition and removal of middleware
  3. Extensible: Easy to implement custom middleware
  4. Thread Safe: Uses Arc for safe sharing of middleware instances
  5. Modular: Built-in middleware implementations can be used independently
  6. Request Modification: Middleware can modify requests before they reach the target service
  7. Error Handling: Middleware can return errors to abort request processing
  8. Order Matters: Middleware is executed in the order they are added to the chain

§Usage

use ri::prelude::*;
use std::sync::Arc;
 
async fn example() -> RiResult<()> {
    // Create a middleware chain
    let mut chain = RiMiddlewareChain::new();
     
    // Add built-in middleware
    chain.add(Arc::new(RiLoggingMiddleware::new("info".to_string())));
    chain.add(Arc::new(RiAuthMiddleware::new("Authorization".to_string())));
    chain.add(Arc::new(RiCorsMiddleware::new(
        vec!["*".to_string()],
        vec!["GET".to_string(), "POST".to_string()],
        vec!["Content-Type".to_string(), "Authorization".to_string()]
    )));
     
    // Create a request and execute the middleware chain
    let mut request = RiGatewayRequest::new("GET".to_string(), "/api/v1/resource".to_string());
    chain.execute(&mut request).await?;
     
    Ok(())
}

Structs§

RiAuthMiddleware
Authentication middleware for validating request credentials.
RiCorsMiddleware
CORS (Cross-Origin Resource Sharing) middleware.
RiLoggingMiddleware
Logging middleware for recording request details.
RiMiddlewareChain
Manages a chain of middleware components.
RiRateLimitMiddleware
Rate limiting middleware for controlling request rates.
RiRequestIdMiddleware
Request ID middleware for processing request IDs.

Traits§

RiMiddleware
Trait defining the middleware interface for request processing.