Skip to main content

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 .py file (excluding __init__.py), it imports the module and looks for a variable named router of type APIRouter.
  • 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

fastapi_autoloader-0.1.4.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_autoloader-0.1.4-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_autoloader-0.1.4.tar.gz.

File metadata

  • Download URL: fastapi_autoloader-0.1.4.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

Hashes for fastapi_autoloader-0.1.4.tar.gz
Algorithm Hash digest
SHA256 8ccfb5551828f8b9928392c877d0d031701e0c3812f62f06725f5f7b2248e478
MD5 6451ef208125fe5aca30be977bad4e4d
BLAKE2b-256 488545950ee06e4ede21a3696811cc08cd16ffec49fbab4e511ab59f2d3e74c8

See more details on using hashes here.

File details

Details for the file fastapi_autoloader-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: fastapi_autoloader-0.1.4-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

Hashes for fastapi_autoloader-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9c7706134a4877be1b3d9329a9fd6de7cc6d72d18d9e6f51ef1995e6d3b189d1
MD5 4a50cb12aabf28c96f8583f26521d295
BLAKE2b-256 f642a5cc8383d0f27c1f25623aa6cd66fd34e7375e094d59aeee8abd06ca8779

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page