
API Routing Module
About 2 min
This file is the core routing management module of the entire application, responsible for unified management and organization of all API interface routes.
File location: mod.rs
Module Structure
Submodule Organization
sys_controll
: System control related routing moduleweb_path
: Web path management tool module
Core Components
WebApi Structure
This is the main entry point for route management, providing static methods to build and manage all routes for the application.
Main Functions
1. Main Route Construction - routers()
Responsible for building the main route collection of the application:
Route Collection Process:
- Create WebPath instance for path management
- Merge routes from the system control module
- Convert path structures into final route configurations
Dynamic API Update Mechanism:
- Asynchronously call InvokeFunctionWorker to execute the "updateapi" task
- Pass the latest route structure information to the background worker process
- Implement dynamic updating and synchronization of routes
Route Registration Process:
- Iterate through all collected path configurations
- Register corresponding HTTP method routes for each valid path
- Build a complete Axum Router instance
2. Whitelist Routes - white_routers()
Specifically used to manage public routes that do not require authentication or special permissions:
- Mainly includes whitelist routes from the system control module
- Used to handle login, health checks, and other public interfaces
Design Features
Modular Architecture
- Organizes routes for different functionalities using submodule separation
- Facilitates maintenance and extension of new API modules
Dynamic Route Management
- Integrates dynamic API update mechanism
- Supports runtime synchronization and updating of route information
Security Layering
- Clearly distinguishes between routes requiring authentication and public routes
- Provides a flexible foundation for permission control
Asynchronous Processing
- Route update operations are executed asynchronously
- Avoids blocking the main route construction process
Extension Guide
Adding New API Modules
- Create a new submodule in the api directory
- Declare the new module in mod.rs
- Merge the routes of the new module in the routers() method
- Add public routes in white_routers() as needed
Best Practices for Route Organization
- Divide submodules according to business functionality
- Maintain consistency and standardization of route paths
- Make reasonable use of the whitelist mechanism to handle public interfaces
This routing management module provides unified, flexible, and extensible API route management capabilities for the entire application and is a core component of the Web service architecture.