|
| 1 | +# FastAPI FS Router |
| 2 | + |
| 3 | +A library for automatically loading routers in FastAPI applications based on file system structure. |
| 4 | + |
| 5 | +English | [한국어](README_ko.md) |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- 📁 Automatically loads routers based on the file system structure |
| 10 | +- 🔗 Maps directory structure directly to API paths |
| 11 | +- 🎯 Detects and registers APIRouter instances automatically |
| 12 | +- ⚙️ Supports custom prefixes for all routes |
| 13 | +- 🚀 Prevents duplicate router registration |
| 14 | +- 🛣️ Supports path parameters and route groups |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +pip install fastapi-fs-router |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Basic Usage |
| 25 | + |
| 26 | +```python |
| 27 | +from fastapi import FastAPI |
| 28 | +from fastapi_fs_router import load_fs_router |
| 29 | + |
| 30 | +app = FastAPI() |
| 31 | + |
| 32 | +# Automatically load all routers from the routers directory |
| 33 | +load_fs_router(app, "routers") |
| 34 | +``` |
| 35 | + |
| 36 | +### Directory Structure Example |
| 37 | + |
| 38 | +``` |
| 39 | +routers/ |
| 40 | +├── users.py # Maps to /users path |
| 41 | +├── items.py # Maps to /items path |
| 42 | +└── v1/ |
| 43 | + └── admin/ |
| 44 | + └── users.py # Maps to /v1/admin/users path |
| 45 | +``` |
| 46 | + |
| 47 | +### Router File Example |
| 48 | + |
| 49 | +```python |
| 50 | +# routers/users.py |
| 51 | +from fastapi import APIRouter |
| 52 | + |
| 53 | +router = APIRouter() |
| 54 | + |
| 55 | +@router.get("/") |
| 56 | +def get_users(): |
| 57 | + return {"users": []} |
| 58 | + |
| 59 | +@router.get("/{user_id}") |
| 60 | +def get_user(user_id: int): |
| 61 | + return {"user_id": user_id} |
| 62 | +``` |
| 63 | + |
| 64 | +### Using Custom Prefix |
| 65 | + |
| 66 | +```python |
| 67 | +from fastapi import FastAPI |
| 68 | +from fastapi_fs_router import load_fs_router |
| 69 | + |
| 70 | +app = FastAPI() |
| 71 | + |
| 72 | +# Add /api/v1 prefix to all routers |
| 73 | +load_fs_router(app, "routers", prefix="/api/v1") |
| 74 | +``` |
| 75 | + |
| 76 | +In this case, routers will be mapped as follows: |
| 77 | +- `routers/users.py` → `/api/v1/users` |
| 78 | +- `routers/v1/admin/users.py` → `/api/v1/v1/admin/users` |
| 79 | +- `routers/(empty)/admin/users.py` → `/api/admin/users` |
| 80 | +- `routers/hello_world/admin/hello_world.py` → `/hello-world/admin/hello-world` |
| 81 | +- `routers/{path_param}/admin.py` → `/{path_param}/admin` |
| 82 | + |
| 83 | +### Path Transformation Rules |
| 84 | + |
| 85 | +- Underscores (`_`) are converted to hyphens (`-`) except for path parameters |
| 86 | +- Square brackets are converted to curly braces (e.g., `[id]` → `{id}`) |
| 87 | +- Parentheses are ignored (e.g., `(empty)`) |
| 88 | + |
| 89 | +## API Reference |
| 90 | + |
| 91 | +### `load_fs_router(app, route_dir, *, prefix="")` |
| 92 | + |
| 93 | +Loads file system-based routers into a FastAPI application. |
| 94 | + |
| 95 | +**Parameters:** |
| 96 | +- `app` (FastAPI): FastAPI application instance |
| 97 | +- `route_dir` (Path | str): Directory path containing router files (default: "routers") |
| 98 | +- `prefix` (str): Prefix to add to all routers (default: "") |
| 99 | + |
| 100 | +**Behavior:** |
| 101 | +1. Recursively traverses the specified directory |
| 102 | +2. Finds `APIRouter` instances in `.py` files |
| 103 | +3. Generates API paths based on directory structure |
| 104 | +4. Registers routers with the FastAPI app |
| 105 | + |
| 106 | +## Development |
| 107 | + |
| 108 | +### Install Dependencies |
| 109 | + |
| 110 | +```bash |
| 111 | +# Install development dependencies |
| 112 | +uv sync |
| 113 | +``` |
| 114 | + |
| 115 | +### Run Tests |
| 116 | + |
| 117 | +```bash |
| 118 | +# Run all tests |
| 119 | +uv run pytest |
| 120 | +``` |
| 121 | + |
| 122 | +### Code Quality Checks |
| 123 | + |
| 124 | +```bash |
| 125 | +# Linting |
| 126 | +ruff check src/ tests/ |
| 127 | + |
| 128 | +# Formatting |
| 129 | +ruff format src/ tests/ |
| 130 | +``` |
| 131 | + |
| 132 | +## License |
| 133 | + |
| 134 | +This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details. |
| 135 | + |
| 136 | +## Contributing |
| 137 | + |
| 138 | +Bug reports, feature requests, and pull requests are welcome! Please create an issue first before contributing. |
| 139 | + |
| 140 | +## Author |
| 141 | + |
| 142 | +- **owjs3901** - *Initial work* - [owjs3901@gmail.com](mailto:owjs3901@gmail.com) |
0 commit comments