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.
§Routing Module
This module provides a flexible routing system for the DMSC gateway, allowing for defining API endpoints and their handlers with support for middleware.
§Key Components
- DMSCRouteHandler: Type alias for route handler functions
- DMSCRoute: Represents a single API route with method, path, handler, and middleware
- DMSCRouter: Manages routes, provides route matching, and supports route mounting
§Design Principles
- Type Safety: Uses type aliases for clear handler signatures
- Middleware Support: Allows attaching middleware to individual routes
- Route Caching: Caches route matches for improved performance
- Flexible Path Matching: Supports exact paths, wildcards, and path parameters
- Method Support: Supports all HTTP methods (GET, POST, PUT, DELETE, PATCH, OPTIONS)
- Route Mounting: Allows mounting routers with prefixes for modularity
- Thread Safe: Uses RwLock for safe operation in multi-threaded environments
- Async Compatibility: Built with async/await patterns for modern Rust applications
§Usage
use dmsc::prelude::*;
use std::sync::Arc;
async fn example() -> DMSCResult<()> {
// Create a router
let router = Arc::new(DMSCRouter::new());
// Create a simple handler
let hello_handler = Arc::new(|req| {
Box::pin(async move {
Ok(DMSCGatewayResponse {
status_code: 200,
headers: HashMap::new(),
body: "Hello, DMSC!".as_bytes().to_vec(),
})
})
});
// Add routes
router.get("/hello", hello_handler.clone());
router.post("/api/v1/users", hello_handler.clone());
// Add route with middleware
let auth_middleware = Arc::new(DMSCAuthMiddleware::new("Authorization".to_string()));
let protected_route = DMSCRoute::new("GET".to_string(), "/api/v1/protected".to_string(), hello_handler)
.with_middleware(auth_middleware);
router.add_route(protected_route);
Ok(())
}Structs§
- DMSC
Route - Represents a single API route with method, path, handler, and middleware.
- DMSC
Router - Router for managing API routes and matching requests to handlers.
Type Aliases§
- DMSC
Route Handler - Type alias for route handler functions.