Skip to main content

Add your description here

Project description

FastAPI FS Router

A library for automatically loading routers in FastAPI applications based on file system structure.

English | 한국어

Features

  • 📁 Automatically loads routers based on the file system structure
  • 🔗 Maps directory structure directly to API paths
  • 🎯 Detects and registers APIRouter instances automatically
  • ⚙️ Supports custom prefixes for all routes
  • 🚀 Prevents duplicate router registration
  • 🛣️ Supports path parameters and route groups

Installation

pip install fastapi-fs-router

Usage

Basic Usage

from fastapi import FastAPI
from fastapi_fs_router import load_fs_router

app = FastAPI()

# Automatically load all routers from the routers directory
load_fs_router(app, "routers")

Directory Structure Example

routers/
├── users.py          # Maps to /users path
├── items.py          # Maps to /items path
└── v1/
    └── admin/
        └── users.py  # Maps to /v1/admin/users path

Router File Example

# routers/users.py
from fastapi import APIRouter

router = APIRouter()

@router.get("/")
def get_users():
    return {"users": []}

@router.get("/{user_id}")
def get_user(user_id: int):
    return {"user_id": user_id}

Using Custom Prefix

from fastapi import FastAPI
from fastapi_fs_router import load_fs_router

app = FastAPI()

# Add /api/v1 prefix to all routers
load_fs_router(app, "routers", prefix="/api/v1")

In this case, routers will be mapped as follows:

  • routers/users.py/api/v1/users
  • routers/v1/admin/users.py/api/v1/v1/admin/users
  • routers/(empty)/admin/users.py/api/admin/users
  • routers/hello_world/admin/hello_world.py/hello-world/admin/hello-world
  • routers/{path_param}/admin.py/{path_param}/admin

Path Transformation Rules

  • Underscores (_) are converted to hyphens (-) except for path parameters
  • Square brackets are converted to curly braces (e.g., [id]{id})
  • Parentheses are ignored (e.g., (empty))

API Reference

load_fs_router(app, route_dir, *, prefix="")

Loads file system-based routers into a FastAPI application.

Parameters:

  • app (FastAPI): FastAPI application instance
  • route_dir (Path | str): Directory path containing router files (default: "routers")
  • prefix (str): Prefix to add to all routers (default: "")

Behavior:

  1. Recursively traverses the specified directory
  2. Finds APIRouter instances in .py files
  3. Generates API paths based on directory structure
  4. Registers routers with the FastAPI app

Development

Install Dependencies

# Install development dependencies
uv sync

Run Tests

# Run all tests
uv run pytest

Code Quality Checks

# Linting
ruff check src/ tests/

# Formatting
ruff format src/ tests/

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Contributing

Bug reports, feature requests, and pull requests are welcome! Please create an issue first before contributing.

Author

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_fs_router-0.1.1.tar.gz (39.2 kB view details)

Uploaded Source

Built Distribution

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

fastapi_fs_router-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_fs_router-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi_fs_router-0.1.1.tar.gz
  • Upload date:
  • Size: 39.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.17

File hashes

Hashes for fastapi_fs_router-0.1.1.tar.gz
Algorithm Hash digest
SHA256 05ddb537cb2d7a2292b18be0c038c2b2dd7ee5e09fe4f691cc2d3d6bbdbcc451
MD5 fd42ad668e2b090b54a82c6bab6a165e
BLAKE2b-256 b9686ae70f7d4dfc07b3675bc454001fb05d3fa0388de71989b2b43bb52dfa67

See more details on using hashes here.

File details

Details for the file fastapi_fs_router-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_fs_router-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6250127e2ea553aa3b3c1d95f4d9395875ed47cfaa46d87f69f29c003901ab87
MD5 13875208baba4ad9394550e0ddfc3b42
BLAKE2b-256 2f7aac001030460372c96d5677ad42943c5f634ba7bea1f4f00b9d07168347bb

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