Skip to content

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

License

Notifications You must be signed in to change notification settings

Luiz-Trindade/fastapi_auto_routes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ FastAPI Auto Routes

Dynamic CRUD & Auth Generator for SQLModel β€” single-file plug-and-play.

FastAPI Python License SQLModel 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

# 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:

poetry add fastapi sqlmodel diskcache

Simply copy or import auto_routes.py into your project.


βš™οΈ Example Usage

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 <token>
      
  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
  • πŸ“§ Contact: Email

⭐ If this file saves you time, give it a star β€” it’s the currency of open source.

About

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

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages