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.3.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.3-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_autoloader-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 942633a014e82b7999fe78130a581673a85561c284b08cb3af9840e0c3ad0e70
MD5 1b1afcd3e9f05933a7857edce40211e3
BLAKE2b-256 3bec6da5ba4b94d7ae7b5a461211bb66701613a9b0724e4e62ad7958cfc2636a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_autoloader-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 913201c564e40f23958e87a397beb98e03a71de0db4e6f84834c48988dc0bb6d
MD5 645a01f59b413e5340b958b22dbcad41
BLAKE2b-256 a0055edd2af6989d1f4677202d26ab78591abd403d8aac2a73c527ccaac0f73e

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