MVC style router autoload
Project description
fastapi-autoloader
A Python package for dynamic routing in FastAPI.
Features
- Automatically loads and registers routers from your controllers directory.
- Supports multilevel directory structures (HMVC-style modules).
- Minimal configuration required.
Installation
pip install fastapi-autoloader
Usage
Suppose your project structure is:
example/
main.py
controllers/
users/
users.py
orders/
orders.py
Each controller module should define a FastAPI APIRouter named router:
example/controllers/users/users.py
from fastapi import APIRouter
router = APIRouter()
@router.get("/users")
def list_users():
return [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
example/controllers/orders/orders.py
from fastapi import APIRouter
router = APIRouter()
@router.get("/orders")
def list_orders():
return [{"id": 1, "item": "Book"}, {"id": 2, "item": "Pen"}]
example/main.py
from fastapi import FastAPI
from fastapi_dynamic_router import DynamicRouter
app = FastAPI()
# Automatically loads all routers from controllers/ and subdirectories
controllers = DynamicRouter("controllers")
controllers.load(app)
@app.get("/")
def root():
return {"message": "Hello"}
Running
Start your app with Uvicorn:
uvicorn example.main:app --reload
How it works
- The package recursively scans the target directory (
controllers/by default). - For each
.pyfile (excluding__init__.py), it imports the module and looks for a variable namedrouterof typeAPIRouter. - All found routers are automatically registered with your FastAPI app.
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_autoloader-0.1.2.tar.gz.
File metadata
- Download URL: fastapi_autoloader-0.1.2.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.13.7 Linux/6.14.0-27-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb59c0b2a233ae19919b29ff652f162be6a75b3705a825eb4deb8b2c855a17ee
|
|
| MD5 |
f55b4d49055a3936881fccc53ef9f43c
|
|
| BLAKE2b-256 |
f0b8649a747ba274f59cce33c220931e1a78fad959138088143bbf1429dbe7f6
|
File details
Details for the file fastapi_autoloader-0.1.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_autoloader-0.1.2-py3-none-any.whl
- Upload date:
- Size: 2.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.13.7 Linux/6.14.0-27-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8f1cfd4d415583f2de13b13087904b2993a40d59e85ab6e1a26e36ee8892f57
|
|
| MD5 |
187919cd5bd2a5dc73bc531d73301122
|
|
| BLAKE2b-256 |
b6265fef09dc765c49567add5cf6b32ed4fd00b2836013a92e8f9f08e4bea80d
|