An open API service indexing awesome lists of open source software.

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 ⚑πŸ”₯

Awesome Lists containing this project

README

          

# ⚑ FastAPI Auto Routes
> Dynamic CRUD & Auth Generator for SQLModel β€” single-file plug-and-play.

[![FastAPI](https://img.shields.io/badge/FastAPI-005571?style=flat&logo=fastapi)](https://fastapi.tiangolo.com/)
[![Python](https://img.shields.io/badge/Python-3.11%2B-blue?logo=python&logoColor=white)](https://www.python.org/)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
[![SQLModel](https://img.shields.io/badge/SQLModel-compatible-success)](https://sqlmodel.tiangolo.com/)
[![DiskCache](https://img.shields.io/badge/diskcache-enabled-orange)](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.