Skip to main content

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

Project description

FastRouter

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 fast_router import fast_router

router = fast_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.2.0.tar.gz (16.3 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.2.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fast_router-0.2.0.tar.gz
  • Upload date:
  • Size: 16.3 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.2.0.tar.gz
Algorithm Hash digest
SHA256 8a8436d6fd452543fd253ba1396afc8c053f43c450c0644aa7e14944475b6fd0
MD5 4ba844cc983abbf5535680101dec7047
BLAKE2b-256 5647505ec34896462d5be47c432777103cff78811a870598f4f3c7da469c7cdc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fast_router-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be173df8c3e1e5b2ab3718d20b4e338bbb36d6fae6a1ac9e54c70b44222a8a0d
MD5 d78518fb4eb918bb784e0aaf3715ea57
BLAKE2b-256 5f9990665415a358329d832cb8c22aceed6c292b0017c3054c633ec3f6238d4a

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