Skip to main content

File-based routing for FastAPI

Project description

00034-3220091456

FastAPI Dynamic Route Loader ๐Ÿš€

Motivation

Sveltekit ruined me ๐Ÿค“! I made this package because:

  1. File-based routing is the bees knees.
  2. I like to watch the world burn.
  3. It's just easier, bro.

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.

HOW TO USE

You can import routes and API Routers as usual, but whenever you want, if you structure your endpoints like below, it will loop over the directory and put all the routes correctly.

Example

๐Ÿ“ project_root/
โ”œ ๐Ÿ“ api/  # This folder is set as the directory in the load_routes function
โ”‚ โ”œ ๐Ÿ“„ route.py  # Translates to /api (base route of the directory)
โ”‚   โ”‚
โ”‚ โ”œ ๐Ÿ“ users/
โ”‚   โ”‚ โ”œ ๐Ÿ“„ route.py  # /api/users
โ”‚   โ”‚ โ”œ ๐Ÿ“„ [user_id].py  # /api/users/{user_id}
โ”‚   โ”‚ โ”” ๐Ÿ“„ profile.py  # /api/users/profile
โ”‚   โ”‚
โ”‚ โ”œ ๐Ÿ“ products/
โ”‚   โ”‚ โ”œ ๐Ÿ“„ route.py  # /api/products
โ”‚   โ”‚ โ”” ๐Ÿ“ [product_id]/
โ”‚   โ”‚  โ”œ ๐Ÿ“„ route.py  # /api/products/{product_id}
โ”‚   โ”‚  โ”” ๐Ÿ“„ reviews.py  # /api/products/{product_id}/reviews
โ”‚   โ”‚
โ”‚ โ”” ๐Ÿ“ settings/
โ”‚  โ”œ ๐Ÿ“„ route.py  # /api/settings
โ”‚  โ”” ๐Ÿ“ notifications/
โ”‚      โ”œ ๐Ÿ“„ route.py  # /api/settings/notifications
โ”‚      โ”” ๐Ÿ“„ email.py  # /api/settings/notifications/email
โ”œ ๐Ÿ“ templates/
โ”‚ โ”” ๐Ÿ“„ home.html  # Not relevant to FastAPI routes
โ”” ๐Ÿ“„ main.py  # Where you set up your FastAPI app and call load_routes

Installation

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

pip install fastapi-file-router

Usage

from fastapi import FastAPI
from fastapi_file_router import load_routes

app = FastAPI()

load_routes(server, "routes", verbose=True)

# Optional
if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        "main:app",
    )

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.14.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

fastapi_file_router-0.1.14-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_file_router-0.1.14.tar.gz
  • Upload date:
  • Size: 4.4 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.14.tar.gz
Algorithm Hash digest
SHA256 31e7399b8d101c666eb7c3f2a84503b1ea33e0e0738b3dc1f60f86acfb5eefd2
MD5 4c2fd69b4c7036ccb2e7e69d307c4172
BLAKE2b-256 ed74d11396dbc5d419812dac8b55cbc593d34ba4c0594c9f7757d3c31afd1213

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_file_router-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 e3d4eaeb20925430294ed1c2e04872f595e200ade144dba019d30b0db5a1af51
MD5 3b099fcd8c010f11f4dbbe1ebb1c431f
BLAKE2b-256 3accd0acc8076ff03401890129fb725c9cf40ea60182bbac0a0fce09d592af3a

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