Skip to main content

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

  1. 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
  1. 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"}
  1. 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_path parameter
  • 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_dir is a relative path: the final path will be base_path/routers_dir
  • If routers_dir is an absolute path: base_path is ignored and routers_dir is 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

fastapi_auto_router-0.2.2.tar.gz (45.0 kB view details)

Uploaded Source

Built Distribution

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

fastapi_auto_router-0.2.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

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

Hashes for fastapi_auto_router-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f5141ad2ed183b656f8e9ede2482781f3dc33133c0abf961f6c82f2616085e04
MD5 6681860c5e39a357a7d08450a6d90fd2
BLAKE2b-256 280242476d3d31cf3da4eaad37dea993896c4366ef875e3b292d38a41a048ca8

See more details on using hashes here.

File details

Details for the file fastapi_auto_router-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_auto_router-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ab1264760af6d820da2ca946181ba94a24dcf6306cb4e7562ac51ef07d68524a
MD5 d43e57297b54646382532a6bdab99b65
BLAKE2b-256 d33364492a3fce1794609a7dff294fbc97e838871d1a3ce78a80e13e04345129

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