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.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 Ri gateway, using a Radix Tree for O(k) route lookup performance, where k is the path length.
§Key Components
- RiRouteHandler: Type alias for route handler functions
- RiRoute: Represents a single API route with method, path, handler, and middleware
- RiRouter: Manages routes using radix trees for efficient O(k) lookup
§Design Principles
- O(k) Performance: Uses radix tree for route matching independent of route count
- 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 (
*path), and path parameters (:param) - 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
§Path Pattern Syntax
/users/:id- Matches/users/123, extractsid = "123"/files/*path- Matches/files/docs/readme.txt, extractspath = "docs/readme.txt"/api/v1/users- Exact match for static paths
§Usage
use ri::prelude::*;
use std::sync::Arc;
async fn example() -> RiResult<()> {
// Create a router
let router = Arc::new(RiRouter::new());
// Create a simple handler
let hello_handler = Arc::new(|req| {
Box::pin(async move {
Ok(RiGatewayResponse {
status_code: 200,
headers: HashMap::new(),
body: "Hello, Ri!".as_bytes().to_vec(),
})
})
});
// Add routes with O(k) lookup performance
router.get("/hello", hello_handler.clone());
router.post("/api/v1/users", hello_handler.clone());
// Add route with path parameter
router.get("/users/:id", hello_handler.clone());
// Add route with wildcard
router.get("/files/*path", hello_handler.clone());
// Add route with middleware
let auth_middleware = Arc::new(RiAuthMiddleware::new("Authorization".to_string()));
let protected_route = RiRoute::new("GET".to_string(), "/api/v1/protected".to_string(), hello_handler)
.with_middleware(auth_middleware);
router.add_route(protected_route);
Ok(())
}Structs§
- RiRoute
- Represents a single API route with method, path, handler, and middleware.
- RiRouter
- Router for managing API routes and matching requests to handlers.
Type Aliases§
- RiRoute
Handler - Type alias for route handler functions.