File-based routing for FastAPI
Project description
FastAPI File-Based Router ๐
Motivation
Sveltekit ruined me ๐ค! I made this package because:
- File-based routing is the bees knees.
- I like to watch the world burn.
- 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/
โ ๐ routes/ # 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("./routes"), 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi_file_router-0.1.15.tar.gz
.
File metadata
- Download URL: fastapi_file_router-0.1.15.tar.gz
- Upload date:
- Size: 4.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27772a897d815bf08ed19e3ad3fe7a1f248c6f27aa35fb9e92b9311dcf9b8cd8 |
|
MD5 | 0600861169218cc6577fdf7c75462500 |
|
BLAKE2b-256 | 68775afd3be513804880eff067204c0bb210af921db02faa97f43644793da92b |
File details
Details for the file fastapi_file_router-0.1.15-py3-none-any.whl
.
File metadata
- Download URL: fastapi_file_router-0.1.15-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.0 Linux/5.15.0-105-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c22b45aba622de6e13d894f15dada31626937470b6faab4ded7896cac49cebe |
|
MD5 | 19e7053b4053d1823891f5b7c5929ace |
|
BLAKE2b-256 | f21888f54ac9cf74b6357f7304d2ff853602d1a7dcf7e11e0d50567a9e96ef53 |