How rakeshgombi/autoapi works
Project Type and Purpose
• Type: Backend API service (FastAPI-based).
• Purpose: Dynamically generate and expose RESTful endpoints for managing “resources” (directories) and “records” (JSON files) located under a configurable root directory.
File Structure Analysis
• Entry Point:
• main.py – initializes the FastAPI app, includes routers, and launches Uvicorn.
• Routing Layer (routes/):
• apis.py – aggregates all route sub-routers into the main FastAPI app.
• endpoints/
• resource_router.py – static endpoints for resource (directory) CRUD.
• dynamic_router.py – logic to discover subdirectories and generate record-level routes at runtime.
• Service Layer (routes/services/):
• resource_service.py – business logic for creating, listing, deleting directories.
• records_service.py – logic for reading, writing, updating, deleting JSON files, plus pagination.
• Utilities (utils/):
• config.py – reads environment (.env) for ROOT_DIR.
• logger.py – configures centralized logging.
• Configuration & Packaging:
• pyproject.toml & requirements.txt – dependency management.
• Dockerfile, .dockerignore – containerization setup.
• .env – environment overrides.
Key Architectural Patterns
• Layered Architecture:
• Presentation (FastAPI routes) → Application Logic (Service layer) → Data Storage (file system).
• Dynamic Route Generation:
• Reflects the file-system structure in API endpoints without hard-coding each route.
• Separation of Concerns:
• Routers only handle HTTP and parameter extraction.
• Services encapsulate file-system interactions.
• Utils handle environment and logging.
Main Components & Interactions (to include
A. Client / API Consumer
• Swagger UI or external HTTP client (curl, Postman)
→ sends HTTP requests (GET/POST/PUT/DELETE)
B. API Gateway / FastAPI App (main.py)
• Receives requests, mounts routers from routes/apis.py.
C. Router Layer
1. ResourceRouter (routes/endpoints/resource_router.py)
• Endpoints: /create-directory/{name}, /list-resources, /delete-resource/{name}
2. DynamicRouter (routes/endpoints/dynamic_router.py)
• Discovers ROOT_DIR contents, auto-registers routes per resource:
• Records: GET /{resource}
• CRUD on individual records: GET/PUT/DELETE /{resource}/{entry_id}, POST /{resource}
D. Service Layer
• ResourceService – Creates, lists, deletes directories under ROOT_DIR.
• RecordsService – Reads/writes JSON files, enforces pagination, entry lookups.
E. File System Storage
• Root directory (configured via utils/config.py + .env)
• Subdirectories = resources; JSON files = records.
F. Utilities
• Config – Environment loading (e.g., ROOT_DIR).
• Logger – Centralized logging pipeline (console/file).
G. External/Infrastructure
• Uvicorn ASGI Server – Hosts the FastAPI app.
• Docker Container – Encapsulates runtime environment.