Module middleware

Module middleware 

Source
Expand description

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

This file is part of DMSC. The DMSC 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 DMSC gateway, allowing for request processing and modification through a chain of middleware components.

§Key Components

  • DMSCMiddleware: Trait defining the middleware interface
  • DMSCMiddlewareChain: 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 dmsc::prelude::*;
use std::sync::Arc;
 
async fn example() -> DMSCResult<()> {
    // Create a middleware chain
    let mut chain = DMSCMiddlewareChain::new();
     
    // Add built-in middleware
    chain.add(Arc::new(DMSCLoggingMiddleware::new("info".to_string())));
    chain.add(Arc::new(DMSCAuthMiddleware::new("Authorization".to_string())));
    chain.add(Arc::new(DMSCCorsMiddleware::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 = DMSCGatewayRequest::new("GET".to_string(), "/api/v1/resource".to_string());
    chain.execute(&mut request).await?;
     
    Ok(())
}

Structs§

DMSCAuthMiddleware
Authentication middleware for validating request credentials.
DMSCCorsMiddleware
CORS (Cross-Origin Resource Sharing) middleware.
DMSCLoggingMiddleware
Logging middleware for recording request details.
DMSCMiddlewareChain
Manages a chain of middleware components.
DMSCRateLimitMiddleware
Rate limiting middleware for controlling request rates.
DMSCRequestIdMiddleware
Request ID middleware for processing request IDs.

Traits§

DMSCMiddleware
Trait defining the middleware interface for request processing.