Skip to main content

File-based routing for FastAPI

Project description

00034-3220091456

FastAPI Dynamic Route Loader 🚀

This Python module dynamically loads FastAPI routes from a specified directory structure. It is designed to streamline the integration of API routes into a FastAPI app, making it easy to manage large applications with many endpoints.

Features

  • Dynamic Route Loading: Automatically load route files as APIRouter instances into your FastAPI application.
  • Path Conversion: Converts square brackets in filenames to curly brackets in route paths to denote path parameters.
  • Verbose Logging: Provides detailed logging to help with debugging during route loading.
  • Auto Tagging: Automatically tags routes in the FastAPI documentation based on their directory path.

Installation

Clone this repository or copy the script into your project directory:

git clone https://github.com/Bewinxed/fastapi-file-router

Ensure that you have FastAPI installed, or install it using pip:

pip install fastapi

Usage

  1. Prepare your route files: Create a directory structure where each Python file represents a FastAPI route. Use route.py for base paths and include other .py files for additional route segments. Use square brackets for dynamic segments in filenames, e.g., [user_id].py.

  2. Load Routes: Import the load_routes function from the module and pass your FastAPI app instance along with the directory containing your route files.

Example directory structure:

/api
    /users
        route.py       # Translates to /users
        [user_id].py   # Translates to /users/{user_id}
    /documents
        /[document_id]
            route.py   # Translates to /documents/{document_id}

  1. Run your app
uvicorn app:app --reload

Logging

Enable verbose logging to get detailed output during the route loading process, which can be helpful for debugging:

load_routes(app, Path("./api"), verbose=True)

Contributing

Contributions are welcome! Please fork the repository and open a pull request with your features or fixes.

License

Don't be a bozo.

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_file_router-0.1.13.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

fastapi_file_router-0.1.13-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_file_router-0.1.13.tar.gz.

File metadata

  • Download URL: fastapi_file_router-0.1.13.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.0 Linux/5.15.0-105-generic

File hashes

Hashes for fastapi_file_router-0.1.13.tar.gz
Algorithm Hash digest
SHA256 812d73cc96531cf31db4aa6b1a0c6bd388937fcb35ccaf79a12559f49f2b8cdd
MD5 3d8c0471e0f0318e68b998c6825f17e2
BLAKE2b-256 f0f079d7b5067da492e1a909ede9355557ac05b074aff90b383fbfdbdf0249ec

See more details on using hashes here.

File details

Details for the file fastapi_file_router-0.1.13-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_file_router-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 a1fa86cd9b54d3aba91766c0b1c14863d9e1362d7b61554a0c4e25e97bb94468
MD5 3f68b17001b7c703b22eccf3184e9cfc
BLAKE2b-256 1cfed9c1b049b4c9a4b49e9fc56ddcb7d8100bed645b0af8d7eba08ab58d262a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page