Skip to main content

A simple and intuitive file-based router for FastAPI applications.

Project description

FileRouter

A powerful, high-performance file-based routing system for FastAPI. It automatically maps your directory structure to API routes using Static Analysis and Lazy Loading.

[!NOTE] You can use this to fuck with your personal or a friends repo but do not push to production :)

🚀 Features

  • Static Analysis: Uses tree-sitter to discover routes without executing your code.
  • Lazy Loading: Route modules are only imported when the first request hits the endpoint.
  • Side-Effect Isolation: Startup is silent. Top-level code in route files only runs on demand.
  • Rich OpenAPI Integration:
    • Automatic Summaries: The first line of your docstring becomes the route summary.
    • Detailed Descriptions: The rest of the docstring becomes the route description.
    • Tag Metadata: Configure directory-level documentation with set_tag_metadata.
  • Flexible Routing:
    • Static: index.py/
    • Dynamic: [id].py/{id}
    • Typed: [id:int].py/{id:int}
    • Slug: [slug:].py/{slug}
    • Catch-all: [...path].py/{path:path}
  • Full FastAPI Support: Works with Depends(), Pydantic models, and all HTTP methods.

🛠️ Quick Start

  1. Install dependencies:

    uv sync
    
  2. Run the demo server:

    PYTHONPATH=src uv run example/main.py
    
  3. Explore the API:

📂 Directory Structure

routes/
├── index.py                 # GET /
├── users/
│   ├── index.py            # GET /users
│   └── [id:int].py        # GET /users/{id}
├── blog/
│   └── [slug:].py         # GET /blog/{slug}
└── files/
    └── [...path].py       # GET /files/{path:path}

📝 Route Handler Example

from fastapi import Query

def get(id: int, q: str = Query(None)):
    """
    Get user by ID.
    
    This description will appear in the expanded section of the 
    OpenAPI documentation, while the first line is the summary.
    """
    return {"user_id": id, "query": q}

⚙️ Advanced Configuration

Tag Metadata

You can customize the documentation for each directory (tag) in your router:

from file_router import file_router

router = file_router("routes")
router.set_tag_metadata(
    "users", 
    description="Operations with users and their profiles.",
    external_docs={"description": "User Guide", "url": "https://example.com/docs"}
)
app = router.get_app()

Smart Fallback

The router is lazy by default. However, if it detects complex FastAPI features (like Depends() or Pydantic models) that require runtime introspection, it automatically falls back to immediate loading for that specific route to ensure 100% compatibility.

🧪 Running Tests

We use pytest for unit and E2E testing, managed via uv.

make test

📜 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

fast_router-0.1.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

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

fast_router-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file fast_router-0.1.0.tar.gz.

File metadata

  • Download URL: fast_router-0.1.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.4

File hashes

Hashes for fast_router-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d61189aff84b93f292ae0da494dcc1c34908e06b5a89038608fd0eef1c58a81a
MD5 6fbda89feaf15fe2bad12e16ef76677d
BLAKE2b-256 a2373563faca16dbdf257d4c7c49c560822be5363de67745b4fb33804665a2b7

See more details on using hashes here.

File details

Details for the file fast_router-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fast_router-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.4

File hashes

Hashes for fast_router-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 711b30c3aafd6703d596e66515230d810abe724ac9f15a09ad0c95daf2ab705b
MD5 b8fe49642cabb3eba91acb9bca6a968a
BLAKE2b-256 95bccadb4152fdec369c15e1974a601baf9184c2aa1ffa6d81f1ddbc09401604

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