How 6g-sandbox/route-manager-api works
Project Type and Purpose
• This is a backend REST API built using FastAPI.
• Its main purpose is to manage network routes on a Linux machine via the “ip” command—with capabilities to query, add, update, pause, and delete routes—and it incorporates a scheduling mechanism that transitions routes through various states (pending, active, paused, expired, deleted). It also includes lifecycle management (a background loop), token-based authentication, and persistence with SQLite.
Main Components
a. API Layer:
• FastAPI server (from app/main.py) that handles HTTP requests.
• Endpoints defined in the routers (app/routers/routes.py) and note that authentication (via services/auth.py) is applied.
b. Service Layer:
• Module blocks for the services (app/services) responsible for:
• Authentication (auth.py)
• Route operations (routes.py)
• Lifecycle management (lifecycle.py that manages scheduled route activation/expiration)
• Utility functions (utils.py) that support operations.
c. Database Layer:
• Database connection and initialization (app/db/database.py).
• Two database models:
• Routes model (app/db/models/routes.py)
• Deleted routes model (app/db/models/deleted_routes.py)
• Additionally, indicate the interaction utilities (app/db/routes.py) that serve to interact with the database.
d. Schemas:
• How request and response data are validated/serialized using Pydantic schemas (app/schemas/routes.py).
e. Configuration and Logging:
• A block for core configuration (app/core/config.py) and global logging settings (app/core/logging.py).
• This highlights where application settings and logging behavior are defined.
f. External Interaction & System Integration:
• Optionally, represent the systemd service integration (as shown in the README’s instructions for running the service) which models production deployment.
Relationships and Interactions
• Client requests → FastAPI server.
• FastAPI server routes incoming HTTP requests to appropriate routers.
• Routers call service functions (e.g., for authentication, lifecycle, or route management) to process business logic.
• Services interact with the database layer for reading or updating route records.
• A continuous background “lifecycle loop” (from app/services/lifecycle.py) checks timestamps and updates route statuses.
• Logging is performed at multiple levels (both in the API layer and service layer) to record operations.
Architectural Patterns
• Separation of concerns: API (routing) layer, service/business logic layer, and the persistence layer.
• Use of FastAPI’s built-in dependency injection for request validation and the Pydantic models.
• Background loop design for lifecycle management, an important non-HTTP workflow in this system.
Relevant Technologies, Frameworks, and Libraries
• FastAPI (API framework)
• Python (core language, version 3.12+)
• SQLite (for persistence)
• Pydantic (for data validation and serialization)
• uv (project management tool managing Python versions and dependencies)
• Systemd (for service management)
• ‘ip’ command (for interaction with the Linux network stack)