Automatic router configuration for FastAPI based on filesystem structure
Project description
FastAPI Auto Router
Automatic router configuration for FastAPI based on filesystem structure.
Installation
pip install fastapi-auto-router
Usage
- Create your router files in a directory structure that matches your desired API paths:
your_project/
└── routers/
└── user_management
├── users.py
└── {user_id}/
└── profile.py
- In your router files, create FastAPI routers:
routers/user_management/users.py
from fastapi import APIRouter
router = APIRouter()
@router.get("")
async def list_users():
return {"message": "List users"}
- In your main FastAPI application, use AutoRouter:
from fastapi import FastAPI
from fastapi_auto_router import AutoRouter
app = FastAPI()
# Initialize and load routers
auto_router = AutoRouter(
app=app,
routers_dir="routers", # Path to your routers directory
api_prefix="/api/v1", # Optional: prefix for all routes
base_path=None # Optional: base path for resolving routers_dir (default: current working directory)
)
auto_router.load_routers()
This will create the following routes:
/api/v1/user-management/users/api/v1/user-management/{user_id}/profile
Features
- Automatic route configuration based on filesystem structure
- Converts underscores to hyphens in route paths
- Supports path parameters using {parameter_name} folders
- Customizable API prefix
- Flexible path resolution with
base_pathparameter - Works with any FastAPI application
Configuration Options
base_path
The base_path parameter allows you to specify the directory where routers_dir is located. This is useful when your router files are not in the current working directory.
Behavior:
- If
routers_diris a relative path: the final path will bebase_path/routers_dir - If
routers_diris an absolute path:base_pathis ignored androuters_diris used as-is
Example:
from fastapi import FastAPI
from fastapi_auto_router import AutoRouter
from pathlib import Path
app = FastAPI()
# Use base_path to specify where routers directory is located
auto_router = AutoRouter(
app=app,
routers_dir="routers",
api_prefix="/api/v1",
base_path=str(Path(__file__).parent) # Resolve routers relative to current file
)
auto_router.load_routers()
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_auto_router-0.2.2.tar.gz.
File metadata
- Download URL: fastapi_auto_router-0.2.2.tar.gz
- Upload date:
- Size: 45.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.24
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5141ad2ed183b656f8e9ede2482781f3dc33133c0abf961f6c82f2616085e04
|
|
| MD5 |
6681860c5e39a357a7d08450a6d90fd2
|
|
| BLAKE2b-256 |
280242476d3d31cf3da4eaad37dea993896c4366ef875e3b292d38a41a048ca8
|
File details
Details for the file fastapi_auto_router-0.2.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_auto_router-0.2.2-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.24
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab1264760af6d820da2ca946181ba94a24dcf6306cb4e7562ac51ef07d68524a
|
|
| MD5 |
d43e57297b54646382532a6bdab99b65
|
|
| BLAKE2b-256 |
d33364492a3fce1794609a7dff294fbc97e838871d1a3ce78a80e13e04345129
|