|
1 | 1 | <h1 align="center"> Benav Labs FastAPI boilerplate</h1> |
2 | 2 | <p align="center" markdown=1> |
3 | | - <i><b>Batteries-included FastAPI starter</b> with production-ready defaults, optional modules, and clear docs.</i> |
| 3 | + <i><b>Batteries-included FastAPI starter</b> — vertical-slice modules, swappable infrastructure, plugin-ready CLI.</i> |
4 | 4 | </p> |
5 | 5 |
|
6 | 6 | <p align="center"> |
|
32 | 32 |
|
33 | 33 | * ⚡️ Fully async FastAPI + SQLAlchemy 2.0 |
34 | 34 | * 🧱 Pydantic v2 models & validation |
35 | | -* 🔐 JWT auth (access + refresh), cookies for refresh |
36 | | -* 👮 Rate limiter + tiers (free/pro/etc.) |
| 35 | +* 🔐 Server-side sessions + CSRF; OAuth (Google wired, GitHub scaffolded); API keys |
| 36 | +* 👮 Rate limiter with per-tier, per-path rules |
37 | 37 | * 🧰 FastCRUD for efficient CRUD & pagination |
38 | | -* 🧑💼 **CRUDAdmin**: minimal admin panel (optional) |
39 | | -* 🚦 ARQ background jobs (Redis) |
40 | | -* 🧊 Redis caching (server + client-side headers) |
41 | | -* 🌐 Configurable CORS middleware for frontend integration |
42 | | -* 🐳 One-command Docker Compose |
43 | | -* 🚀 NGINX & Gunicorn recipes for prod |
| 38 | +* 🧑💼 **SQLAdmin**-based admin panel (optional, env-toggled) |
| 39 | +* 🚦 [Taskiq](https://taskiq-python.github.io/) workers (Redis or RabbitMQ broker) |
| 40 | +* 🧊 Redis or Memcached caching (`@cache` decorator + provider API) |
| 41 | +* 🛠 **Plugin-ready `bp` CLI** — generate compose files, audit env, mount third-party command/feature plugins |
| 42 | +* 🐳 Docker Compose for local / prod / nginx-fronted (generated by the CLI) |
44 | 43 |
|
45 | 44 | ## Why and When to use it |
46 | 45 |
|
47 | 46 | **Perfect if you want:** |
48 | 47 |
|
49 | 48 | * A pragmatic starter with auth, CRUD, jobs, caching and rate-limits |
50 | 49 | * **Sensible defaults** with the freedom to opt-out of modules |
51 | | -* **Docs over boilerplate** in README - depth lives in the site |
| 50 | +* **A foundation that grows** — vertical-slice modules + a plugin-aware CLI for code generators |
| 51 | +* **Docs over boilerplate** in this README — depth lives on the [docs site](https://benavlabs.github.io/FastAPI-boilerplate/) |
52 | 52 |
|
53 | | -> **Not a fit** if you need a monorepo microservices scaffold - [see the docs](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/project-structure/) for pointers. |
| 53 | +> **Not a fit** if you need a monorepo microservices scaffold — [see the docs](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/project-structure/) for pointers. |
54 | 54 |
|
55 | 55 | **What you get:** |
56 | 56 |
|
57 | | -* **App**: FastAPI app factory, [env-aware docs](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/development/) exposure |
58 | | -* **Auth**: [JWT access/refresh](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/authentication/), logout via token blacklist |
59 | | -* **DB**: Postgres + SQLAlchemy 2.0, [Alembic migrations](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/database/) |
60 | | -* **CRUD**: [FastCRUD generics](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/database/crud/) (get, get_multi, create, update, delete, joins) |
61 | | -* **Caching**: [decorator-based endpoints cache](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/caching/); client cache headers |
62 | | -* **Queues**: [ARQ worker](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/background-tasks/) (async jobs), Redis connection helpers |
| 57 | +* **App**: FastAPI [app factory](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/project-structure/), env-aware docs exposure |
| 58 | +* **Auth**: [server-side sessions](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/authentication/sessions/), CSRF, [OAuth](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/authentication/), [API keys](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/authentication/permissions/) |
| 59 | +* **DB**: Postgres + SQLAlchemy 2.0, [Alembic migrations](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/database/migrations/) with prod-confirm gate |
| 60 | +* **CRUD**: [FastCRUD generics](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/database/crud/) |
| 61 | +* **Caching**: [decorator + provider API](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/caching/) (Redis or Memcached) |
| 62 | +* **Queues**: [Taskiq workers](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/background-tasks/) (Redis or RabbitMQ) |
63 | 63 | * **Rate limits**: [per-tier + per-path rules](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/rate-limiting/) |
64 | | -* **Admin**: [CRUDAdmin views](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/admin-panel/) for common models (optional) |
| 64 | +* **Admin**: [SQLAdmin views](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/admin-panel/) (optional, env-toggled) |
| 65 | +* **CLI**: [`bp` tool](https://benavlabs.github.io/FastAPI-boilerplate/cli/) for compose scaffolding, env audits, and plugin extensions |
65 | 66 |
|
66 | | -This is what we've been using in production apps. Several applications running in production started from this boilerplate as their foundation - from SaaS platforms to internal tools. It's proven, stable technology that works together reliably. Use this as the foundation for whatever you want to build on top. |
| 67 | +> **Building an AI SaaS?** Skip even more setup with [**FastroAI**](https://fastro.ai) — our production-ready template with AI integration, payments, and frontend included. |
67 | 68 |
|
68 | | -> **Building an AI SaaS?** Skip even more setup with [**FastroAI**](https://fastro.ai) - our production-ready template with AI integration, payments, and frontend included. |
| 69 | +## Repo Layout |
69 | 70 |
|
70 | | -## TL;DR - Quickstart |
| 71 | +This is a [uv workspace](https://docs.astral.sh/uv/concepts/projects/workspaces/) with two members. One venv at the root covers both. |
71 | 72 |
|
72 | | -Use the template on GitHub, create your repo, then: |
| 73 | +```text |
| 74 | +fastapi-boilerplate/ |
| 75 | +├── pyproject.toml # workspace root (uv workspace metadata) |
| 76 | +├── backend/ # the deployable application |
| 77 | +│ ├── src/ # interfaces/, infrastructure/, modules/ |
| 78 | +│ ├── pyproject.toml |
| 79 | +│ └── Dockerfile # multi-stage: dev / migrate / prod |
| 80 | +└── cli/ # `bp` — developer/operator tool (never ships in prod) |
| 81 | + └── src/cli/ |
| 82 | +``` |
| 83 | + |
| 84 | +## Quickstart |
73 | 85 |
|
74 | 86 | ```bash |
75 | 87 | git clone https://github.com/<you>/FastAPI-boilerplate |
76 | 88 | cd FastAPI-boilerplate |
| 89 | +uv sync --all-extras # one venv at the root, both members installed |
77 | 90 | ``` |
78 | 91 |
|
79 | | -**Quick setup:** Run the interactive setup script to choose your deployment configuration: |
| 92 | +Generate a compose file for the deployment shape you want: |
80 | 93 |
|
81 | 94 | ```bash |
82 | | -./setup.py |
| 95 | +uv run bp deploy generate local # hot-reload dev stack |
| 96 | +# or: uv run bp deploy generate prod # production single-host |
| 97 | +# or: uv run bp deploy generate nginx # production behind nginx |
83 | 98 | ``` |
84 | 99 |
|
85 | | -Or directly specify the deployment type: `./setup.py local`, `./setup.py staging`, or `./setup.py production`. |
86 | | - |
87 | | -The script copies the right files for your deployment scenario. Here's what each option sets up: |
88 | | - |
89 | | -### Option 1: Local development with Uvicorn |
90 | | - |
91 | | -Best for: **Development and testing** |
92 | | - |
93 | | -**Copies:** |
94 | | - |
95 | | -- `scripts/local_with_uvicorn/Dockerfile` → `Dockerfile` |
96 | | -- `scripts/local_with_uvicorn/docker-compose.yml` → `docker-compose.yml` |
97 | | -- `scripts/local_with_uvicorn/.env.example` → `src/.env` |
98 | | - |
99 | | -Sets up Uvicorn with auto-reload enabled. The example environment values work fine for development. |
100 | | - |
101 | | -**Manual setup:** `./setup.py local` or copy the files above manually. |
102 | | - |
103 | | -### Option 2: Staging with Gunicorn managing Uvicorn workers |
104 | | - |
105 | | -Best for: **Staging environments and load testing** |
106 | | - |
107 | | -**Copies:** |
108 | | - |
109 | | -- `scripts/gunicorn_managing_uvicorn_workers/Dockerfile` → `Dockerfile` |
110 | | -- `scripts/gunicorn_managing_uvicorn_workers/docker-compose.yml` → `docker-compose.yml` |
111 | | -- `scripts/gunicorn_managing_uvicorn_workers/.env.example` → `src/.env` |
112 | | - |
113 | | -Sets up Gunicorn managing multiple Uvicorn workers for production-like performance testing. |
114 | | - |
115 | | -> [!WARNING] |
116 | | -> Change `SECRET_KEY` and passwords in the `.env` file for staging environments. |
117 | | -
|
118 | | -**Manual setup:** `./setup.py staging` or copy the files above manually. |
119 | | - |
120 | | -### Option 3: Production with NGINX |
121 | | - |
122 | | -Best for: **Production deployments** |
123 | | - |
124 | | -**Copies:** |
125 | | - |
126 | | -- `scripts/production_with_nginx/Dockerfile` → `Dockerfile` |
127 | | -- `scripts/production_with_nginx/docker-compose.yml` → `docker-compose.yml` |
128 | | -- `scripts/production_with_nginx/.env.example` → `src/.env` |
129 | | - |
130 | | -Sets up NGINX as reverse proxy with Gunicorn + Uvicorn workers for production. |
131 | | - |
132 | | -> [!CAUTION] |
133 | | -> You MUST change `SECRET_KEY`, all passwords, and sensitive values in the `.env` file before deploying! |
134 | | -
|
135 | | -**Manual setup:** `./setup.py production` or copy the files above manually. |
136 | | - |
137 | | ---- |
138 | | - |
139 | | -**Start your application:** |
| 100 | +Configure your env (the CLI helps with secrets and validation): |
140 | 101 |
|
141 | 102 | ```bash |
142 | | -docker compose up |
| 103 | +cp backend/.env.example backend/.env |
| 104 | +uv run bp env gen-secret # print a fresh SECRET_KEY |
| 105 | +uv run bp env validate # audit .env against the production validator |
143 | 106 | ``` |
144 | 107 |
|
145 | | -**Access your app:** |
146 | | -- **Local**: http://127.0.0.1:8000 (auto-reload enabled) → [API docs](http://127.0.0.1:8000/docs) |
147 | | -- **Staging**: http://127.0.0.1:8000 (production-like performance) |
148 | | -- **Production**: http://localhost (NGINX reverse proxy) |
149 | | - |
150 | | -### Next steps |
151 | | - |
152 | | -**Create your first admin user:** |
153 | | -```bash |
154 | | -docker compose run --rm create_superuser |
155 | | -``` |
| 108 | +Bring it up: |
156 | 109 |
|
157 | | -**Run database migrations** (if you add models): |
158 | 110 | ```bash |
159 | | -cd src && uv run alembic revision --autogenerate && uv run alembic upgrade head |
| 111 | +docker compose up --build |
| 112 | +# → http://127.0.0.1:8000 (Swagger at /docs) |
160 | 113 | ``` |
161 | 114 |
|
162 | | -**Test background jobs:** |
163 | | -```bash |
164 | | -curl -X POST 'http://127.0.0.1:8000/api/v1/tasks/task?message=hello' |
165 | | -``` |
| 115 | +**Without Docker** (Postgres + Redis required locally): |
166 | 116 |
|
167 | | -**Or run locally without Docker:** |
168 | 117 | ```bash |
169 | | -uv sync && uv run uvicorn src.app.main:app --reload |
| 118 | +cd backend |
| 119 | +uv run alembic upgrade head |
| 120 | +uv run python -m scripts.setup_initial_data # creates the first admin user + default tier |
| 121 | +uv run fastapi dev src/interfaces/main.py # API |
| 122 | +uv run taskiq worker infrastructure.taskiq.worker:default_broker # in a second terminal |
170 | 123 | ``` |
171 | 124 |
|
172 | | -> Full setup (from-scratch, .env examples, PostgreSQL & Redis, gunicorn, nginx) lives in the [docs](https://benavlabs.github.io/FastAPI-boilerplate/getting-started/installation/). |
173 | | -
|
174 | | -## Configuration (minimal) |
175 | | - |
176 | | -Create `src/.env` and set **app**, **database**, **JWT**, and **environment** settings. See the [docs](https://benavlabs.github.io/FastAPI-boilerplate/getting-started/configuration/) for a copy-pasteable example and production guidance. |
177 | | - |
178 | | -[https://benavlabs.github.io/FastAPI-boilerplate/getting-started/configuration/](https://benavlabs.github.io/FastAPI-boilerplate/getting-started/configuration/) |
179 | | - |
180 | | -* `ENVIRONMENT=local|staging|production` controls API docs exposure |
181 | | -* Set `ADMIN_*` to enable the first admin user |
| 125 | +> Full setup, env-var reference, and per-environment deployment guides live in the [docs](https://benavlabs.github.io/FastAPI-boilerplate/getting-started/installation/). |
182 | 126 |
|
183 | 127 | ## Common tasks |
184 | 128 |
|
185 | 129 | ```bash |
186 | | -# run locally with reload (without Docker) |
187 | | -uv sync && uv run uvicorn src.app.main:app --reload |
| 130 | +# generate a fresh production-ready compose file |
| 131 | +uv run bp deploy generate prod --workers 8 |
| 132 | + |
| 133 | +# audit your .env against the production security validator |
| 134 | +uv run bp env validate |
188 | 135 |
|
189 | 136 | # run Alembic migrations |
190 | | -cd src && uv run alembic revision --autogenerate && uv run alembic upgrade head |
| 137 | +cd backend && uv run alembic revision --autogenerate -m "<msg>" && uv run alembic upgrade head |
| 138 | + |
| 139 | +# run tests |
| 140 | +cd backend && uv run pytest |
191 | 141 |
|
192 | | -# enqueue a background job (example endpoint) |
193 | | -curl -X POST 'http://127.0.0.1:8000/api/v1/tasks/task?message=hello' |
| 142 | +# install bp as a global tool (optional) |
| 143 | +uv tool install --editable ./cli |
194 | 144 | ``` |
195 | 145 |
|
196 | | -More examples (superuser creation, tiers, rate limits, admin usage) in the [docs](https://benavlabs.github.io/FastAPI-boilerplate/getting-started/first-run/). |
| 146 | +More examples (superuser creation, tiers, rate limits, admin usage, plugin authoring) in the [docs](https://benavlabs.github.io/FastAPI-boilerplate/). |
197 | 147 |
|
198 | 148 | ## Contributing |
199 | 149 |
|
|
0 commit comments