https://github.com/luiz-trindade/fastapi_auto_routes
β‘ FastAPI Auto Routes β a smart CRUD & Auth generator for SQLModel π It builds secure, cache-powered REST APIs π with login, token-based auth πͺͺ, and async concurrency βοΈ. Cut boilerplate, boost productivity, and ship production-ready backends fast β‘π₯
https://github.com/luiz-trindade/fastapi_auto_routes
api-generator async asyncio auth backend backend-development caching concurrency crud fastapi jwt microservice pydantic python rest-api scalable-api sqlite sqlmodel sqlmodel-crud token-based-auth
Last synced: about 2 months ago
JSON representation
β‘ FastAPI Auto Routes β a smart CRUD & Auth generator for SQLModel π It builds secure, cache-powered REST APIs π with login, token-based auth πͺͺ, and async concurrency βοΈ. Cut boilerplate, boost productivity, and ship production-ready backends fast β‘π₯
- Host: GitHub
- URL: https://github.com/luiz-trindade/fastapi_auto_routes
- Owner: Luiz-Trindade
- License: mit
- Created: 2025-10-05T15:58:20.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-10-05T16:21:27.000Z (8 months ago)
- Last Synced: 2025-10-05T17:48:17.316Z (8 months ago)
- Topics: api-generator, async, asyncio, auth, backend, backend-development, caching, concurrency, crud, fastapi, jwt, microservice, pydantic, python, rest-api, scalable-api, sqlite, sqlmodel, sqlmodel-crud, token-based-auth
- Language: Python
- Homepage:
- Size: 20.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# β‘ FastAPI Auto Routes
> Dynamic CRUD & Auth Generator for SQLModel β single-file plug-and-play.
[](https://fastapi.tiangolo.com/)
[](https://www.python.org/)
[](LICENSE)
[](https://sqlmodel.tiangolo.com/)
[](https://grantjenks.com/docs/diskcache/)
---
## π§ Overview
**FastAPI Auto Routes** is a **single-file dynamic router generator** (`auto_routes.py`) that eliminates repetitive CRUD boilerplate.
Simply download or import the file, configure your **SQLModel engine**, and youβre ready to generate full-featured **CRUD endpoints** with:
- β
Authentication via Bearer tokens
- β‘ Smart caching (with TTL)
- π Concurrency control
- π§© Bulk operations
- πͺͺ Auto-generated `/login` and `/logout` routes
Built on **FastAPI + SQLModel + diskcache**, ready to plug into your project.
---
## π Installation
```bash
# Clone the repo
git clone https://github.com/yourusername/fastapi-auto-routes.git
cd fastapi-auto-routes
# Install dependencies
pip install -r requirements.txt
```
or using **Poetry**:
```bash
poetry add fastapi sqlmodel diskcache
```
Simply copy or import `auto_routes.py` into your project.
---
## βοΈ Example Usage
```python
from fastapi import FastAPI
from sqlmodel import SQLModel, Field
from app.db.config import engine # Configure your SQLModel engine
from app.utils.auto_routes import crud_router
class User(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
email: str
password: str
# Create tables
SQLModel.metadata.create_all(engine)
app = FastAPI()
# π Auth Router (Login / Logout)
app.include_router(
crud_router(User, login=True, login_fields=["email", "password"]),
prefix="/auth",
tags=["Auth"]
)
# βοΈ CRUD Router (Requires Token)
app.include_router(
crud_router(User, auth=True, ttl=120, max_concurrent=8),
prefix="/users",
tags=["Users"]
)
```
---
## π§© Generated Routes
| Route | Method | Description | Auth Required |
| -------------- | -------- | ------------------------- | ------------- |
| `/auth/login` | `POST` | Generate session token | No |
| `/auth/logout` | `POST` | Invalidate active session | β
|
| `/users/` | `GET` | Paginated list of users | β
|
| `/users/{id}` | `GET` | Get user by ID | β
|
| `/users/` | `POST` | Create user | β
|
| `/users/{id}` | `PATCH` | Update user | β
|
| `/users/{id}` | `DELETE` | Delete user | β
|
---
## β‘ Parameters
| Parameter | Type | Default | Description |
| ---------------- | ---------------- | ------------- | ------------------------------------ |
| `model` | `Type[SQLModel]` | β | Your SQLModel class |
| `ttl` | `int \| None` | `None` | Cache expiration time (seconds) |
| `max_concurrent` | `int \| None` | `cpu_count()` | Max concurrent operations |
| `login` | `bool` | `False` | Enables `/login` and `/logout` |
| `login_fields` | `List[str]` | `None` | Fields used for login validation |
| `login_ttl` | `int` | `3600` | Token lifetime in seconds |
| `auth` | `bool` | `False` | Requires Bearer token for all routes |
---
## π§ How It Works
1. **Single-file CRUD & Auth Generation**
`crud_router()` dynamically builds all routes (`GET`, `POST`, `PATCH`, `DELETE`) for the given model from **one file**.
2. **Authentication Layer**
* `/login`: validates credentials and creates a token stored in `sessions_cache`.
* `/logout`: invalidates the token.
* Protected routes require the header:
```
Authorization: Bearer
```
3. **Caching & Concurrency**
* Uses `diskcache` for persistent caching with optional TTL.
* Uses `asyncio.Semaphore` for safe concurrency limits per model.
---
## π Project Structure
```
app/
βββ db/
β βββ config.py # Database engine setup
βββ utils/
β βββ auto_routes.py # Single-file router generator
βββ main.py # FastAPI entrypoint
```
---
## π§° Requirements
* Python 3.11+
* FastAPI
* SQLModel
* DiskCache
* Uvicorn (for local testing)
---
## π§ββοΈ Philosophy
> **Automation without compromise.**
Instead of repeating CRUD definitions across every model, this **single file** dynamically builds routers that are **secure**, **scalable**, and **production-ready**.
Your backend becomes **data-driven**, not boilerplate-driven.
---
## π License
MIT License Β© 2025 Luiz Gabriel MagalhΓ£es Trindade
Free for personal and commercial use.
---
## π Connect
* π§ **Project Author:** Luiz Gabriel Trindade
* πΌ GitHub: [@Luiz-Trindade](https://github.com/Luiz-Trindade)
* π§ Contact: [Email](mailto:luiz.gabriel.m.trindade@gmail.com)
---
### β If this file saves you time, give it a star β itβs the currency of open source.