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.0Unless 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
- Async Trait: Uses async_trait for async middleware execution
- Flexible Chain: Allows dynamic addition and removal of middleware
- Extensible: Easy to implement custom middleware
- Thread Safe: Uses Arc for safe sharing of middleware instances
- Modular: Built-in middleware implementations can be used independently
- Request Modification: Middleware can modify requests before they reach the target service
- Error Handling: Middleware can return errors to abort request processing
- 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§
- DMSC
Auth Middleware - Authentication middleware for validating request credentials.
- DMSC
Cors Middleware - CORS (Cross-Origin Resource Sharing) middleware.
- DMSC
Logging Middleware - Logging middleware for recording request details.
- DMSC
Middleware Chain - Manages a chain of middleware components.
- DMSC
Rate Limit Middleware - Rate limiting middleware for controlling request rates.
- DMSC
Request IdMiddleware - Request ID middleware for processing request IDs.
Traits§
- DMSC
Middleware - Trait defining the middleware interface for request processing.