|
| 1 | +# Benav Labs FastAPI Boilerplate |
| 2 | + |
| 3 | +> **Batteries-included FastAPI starter** with Pydantic v2, SQLAlchemy 2.0, PostgreSQL, Redis, ARQ jobs, rate-limiting and a minimal admin. Production-ready defaults, optional modules, and clear docs. |
| 4 | +
|
| 5 | +<p align="left"> |
| 6 | + <a href="https://fastapi.tiangolo.com">FastAPI</a> |
| 7 | + <a href="https://docs.pydantic.dev">Pydantic v2</a> |
| 8 | + <a href="https://docs.sqlalchemy.org/en/20/">SQLAlchemy 2.0</a> |
| 9 | + <a href="https://www.postgresql.org">PostgreSQL</a> |
| 10 | + <a href="https://redis.io">Redis</a> |
| 11 | + <a href="https://arq-docs.helpmanual.io">ARQ</a> |
| 12 | +</p> |
| 13 | + |
| 14 | +**Docs:** |
| 15 | + |
| 16 | +* 📚 [https://benavlabs.github.io/FastAPI-boilerplate/](https://benavlabs.github.io/FastAPI-boilerplate/) |
| 17 | +* 🧠 DeepWiki: [https://deepwiki.com/benavlabs/FastAPI-boilerplate](https://deepwiki.com/benavlabs/FastAPI-boilerplate) |
| 18 | +* 💬 Discord: [https://discord.com/invite/TEmPs22gqB](https://discord.com/invite/TEmPs22gqB) |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## TL;DR - Quickstart |
| 23 | + |
| 24 | +Use the template on GitHub, create your repo, then: |
| 25 | + |
| 26 | +```bash |
| 27 | +# Clone your new repository |
| 28 | +git clone https://github.com/<you>/<your-app> |
| 29 | +cd <your-app> |
| 30 | + |
| 31 | +# NOTE (added by me): |
| 32 | +# Running locally with Uvicorn. |
| 33 | +# The .env and docker-compose.yml files were taken from this Gist: |
| 34 | +# https://gist.github.com/igorbenav/48ad745120c3f77817e094f3a609111a |
| 35 | +# I kept the local Dockerfile since it uses 'uv' instead of Poetry |
| 36 | +# (the Gist version relies on Poetry). |
| 37 | + |
| 38 | +# TODO: Decide where to put the example file, since it is currently |
| 39 | +# being copied from the Gist. |
| 40 | + |
| 41 | +# Copy and create your environment file |
| 42 | +cp src/.env.example src/.env |
| 43 | +# Fill in the minimal environment variables as described in the docs |
| 44 | + |
| 45 | +# Run everything using Docker |
| 46 | +docker compose up |
| 47 | + |
| 48 | +# Open the API documentation |
| 49 | +open http://127.0.0.1:8000/docs |
| 50 | +``` |
| 51 | + |
| 52 | +> Full setup (from-scratch, .env examples, PostgreSQL & Redis, gunicorn, nginx) lives in the docs. |
| 53 | +
|
| 54 | +--- |
| 55 | + |
| 56 | +## Features |
| 57 | + |
| 58 | +* ⚡️ Fully async FastAPI + SQLAlchemy 2.0 |
| 59 | +* 🧱 Pydantic v2 models & validation |
| 60 | +* 🔐 JWT auth (access + refresh), cookies for refresh |
| 61 | +* 👮 Rate limiter + tiers (free/pro/etc.) |
| 62 | +* 🧰 FastCRUD for efficient CRUD & pagination |
| 63 | +* 🧑💼 **CRUDAdmin**: minimal admin panel (optional) |
| 64 | +* 🚦 ARQ background jobs (Redis) |
| 65 | +* 🧊 Redis caching (server + client-side headers) |
| 66 | +* 🐳 One-command Docker Compose |
| 67 | +* 🚀 NGINX & Gunicorn recipes for prod |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## When to use it |
| 72 | + |
| 73 | +* You want a pragmatic starter with auth, CRUD, jobs, caching and rate-limits. |
| 74 | +* You value **sensible defaults** with the freedom to opt-out of modules. |
| 75 | +* You prefer **docs over boilerplate** in README - depth lives in the site. |
| 76 | + |
| 77 | +Not a fit if you need a monorepo microservices scaffold - see the docs for pointers. |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## What's inside (high-level) |
| 82 | + |
| 83 | +* **App**: FastAPI app factory, env-aware docs exposure |
| 84 | +* **Auth**: JWT access/refresh, logout via token blacklist |
| 85 | +* **DB**: Postgres + SQLAlchemy 2.0, Alembic migrations |
| 86 | +* **CRUD**: FastCRUD generics (get, get_multi, create, update, delete, joins) |
| 87 | +* **Caching**: decorator-based endpoints cache; client cache headers |
| 88 | +* **Queues**: ARQ worker (async jobs), Redis connection helpers |
| 89 | +* **Rate limits**: per-tier + per-path rules |
| 90 | +* **Admin**: CRUDAdmin views for common models (optional) |
| 91 | + |
| 92 | +> The full tree and deep dives are in **Project Structure**, **Database**, **CRUD Operations**, **API**, **Caching**, **Background Tasks**, **Rate Limiting**, and **Production** sections of the docs. |
| 93 | +
|
| 94 | +--- |
| 95 | + |
| 96 | +## Configuration (minimal) |
| 97 | + |
| 98 | +Create `src/.env` and set **app**, **database**, **JWT**, and **environment** settings. See the docs for a copy-pasteable example and production guidance. |
| 99 | + |
| 100 | +* `ENVIRONMENT=local|staging|production` controls API docs exposure |
| 101 | +* Set `ADMIN_*` to enable the first admin user |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## Common tasks |
| 106 | + |
| 107 | +```bash |
| 108 | +# run locally with reload (without Docker) |
| 109 | +uv sync && uv run uvicorn src.app.main:app --reload |
| 110 | + |
| 111 | +# run Alembic migrations |
| 112 | +cd src && uv run alembic revision --autogenerate && uv run alembic upgrade head |
| 113 | + |
| 114 | +# enqueue a background job (example endpoint) |
| 115 | +curl -X POST 'http://127.0.0.1:8000/api/v1/tasks/task?message=hello' |
| 116 | +``` |
| 117 | + |
| 118 | +More examples (superuser creation, tiers, rate limits, admin usage) - **docs**. |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## Contributing |
| 123 | + |
| 124 | +Issues and PRs are welcome. Please read **CONTRIBUTING.md** and follow the style of existing modules (type hints, async/await, explicit None checks, and paginated responses). |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## License |
| 129 | + |
| 130 | +MIT - see `LICENSE.md`. |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +<p align="center"> |
| 135 | + <a href="https://benav.io"> |
| 136 | + <img src="https://github.com/benavlabs/fastcrud/raw/main/docs/assets/benav_labs_banner.png" alt="Powered by Benav Labs - benav.io" width="420"/> |
| 137 | + </a> |
| 138 | +</p> |
0 commit comments