Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
11 changes: 0 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,3 @@ cython_debug/
.idea

.vscode/

# Config files:
src/.env

# Ignore root files:
/Dockerfile
/docker-compose.yml

# Don't ignore files inside of script folder:
!scripts/*

2 changes: 0 additions & 2 deletions scripts/local_with_uvicorn/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,4 @@ USER app
# Set the working directory
WORKDIR /code

# -------- replace with comment to run with gunicorn --------
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--reload-include", ".env"]
# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000"]
140 changes: 37 additions & 103 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ALMOST ALL DOCUMENTATION WILL HAVE TO BE UPDATED TO REFLECT THE CHANGES AND THE SIMPLIFICATION OF BEING CONTAINER FIRST CONFIGURATION, BETTER TO DO THIS AFTER THE CODE IS DONE AND AGREED UPON. I STARTED BUT I DO NOT WANT TO REPEAT OR REDO SECTIONS SO BETTER TO WAIT ON THE FEEDBACK ON CODE CHANGES I THINK.

<h1 align="center"> Benav Labs FastAPI boilerplate</h1>
<p align="center" markdown=1>
<i><b>Batteries-included FastAPI starter</b> with production-ready defaults, optional modules, and clear docs.</i>
Expand Down Expand Up @@ -30,143 +32,75 @@

## Features

* ⚡️ Fully async FastAPI + SQLAlchemy 2.0
* 🧱 Pydantic v2 models & validation
* 🔐 JWT auth (access + refresh), cookies for refresh
* 👮 Rate limiter + tiers (free/pro/etc.)
* 🧰 FastCRUD for efficient CRUD & pagination
* 🧑‍💼 **CRUDAdmin**: minimal admin panel (optional)
* 🚦 ARQ background jobs (Redis)
* 🧊 Redis caching (server + client-side headers)
* 🌐 Configurable CORS middleware for frontend integration
* 🐳 One-command Docker Compose
* 🚀 NGINX & Gunicorn recipes for prod
- ⚡️ Fully async FastAPI + SQLAlchemy 2.0
- 🧱 Pydantic v2 models & validation
- 🔐 JWT auth (access + refresh), cookies for refresh
- 👮 Rate limiter + tiers (free/pro/etc.)
- 🧰 FastCRUD for efficient CRUD & pagination
- 🧑‍💼 **CRUDAdmin**: minimal admin panel (optional)
- 🚦 ARQ background jobs (Redis)
- 🧊 Redis caching (server + client-side headers)
- 🌐 Configurable CORS middleware for frontend integration
- 🐳 One-command Docker Compose
- 🚀 Optimized for containarized deployments

## Why and When to use it

**Perfect if you want:**

* A pragmatic starter with auth, CRUD, jobs, caching and rate-limits
* **Sensible defaults** with the freedom to opt-out of modules
* **Docs over boilerplate** in README - depth lives in the site
- A pragmatic starter with auth, CRUD, jobs, caching and rate-limits
- **Sensible defaults** with the freedom to opt-out of modules
- **Docs over boilerplate** in README - depth lives in the site

> **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.

**What you get:**

* **App**: FastAPI app factory, [env-aware docs](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/development/) exposure
* **Auth**: [JWT access/refresh](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/authentication/), logout via token blacklist
* **DB**: Postgres + SQLAlchemy 2.0, [Alembic migrations](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/database/)
* **CRUD**: [FastCRUD generics](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/database/crud/) (get, get_multi, create, update, delete, joins)
* **Caching**: [decorator-based endpoints cache](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/caching/); client cache headers
* **Queues**: [ARQ worker](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/background-tasks/) (async jobs), Redis connection helpers
* **Rate limits**: [per-tier + per-path rules](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/rate-limiting/)
* **Admin**: [CRUDAdmin views](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/admin-panel/) for common models (optional)
- **App**: FastAPI app factory, [env-aware docs](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/development/) exposure
- **Auth**: [JWT access/refresh](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/authentication/), logout via token blacklist
- **DB**: Postgres + SQLAlchemy 2.0, [Alembic migrations](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/database/)
- **CRUD**: [FastCRUD generics](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/database/crud/) (get, get_multi, create, update, delete, joins)
- **Caching**: [decorator-based endpoints cache](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/caching/); client cache headers
- **Queues**: [ARQ worker](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/background-tasks/) (async jobs), Redis connection helpers
- **Rate limits**: [per-tier + per-path rules](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/rate-limiting/)
- **Admin**: [CRUDAdmin views](https://benavlabs.github.io/FastAPI-boilerplate/user-guide/admin-panel/) for common models (optional)

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.

> **Building an AI SaaS?** Skip even more setup with [**FastroAI**](https://fastro.ai) - our production-ready template with AI integration, payments, and frontend included.

## TL;DR - Quickstart

Use the template on GitHub, create your repo, then:

```bash
git clone https://github.com/<you>/FastAPI-boilerplate
cd FastAPI-boilerplate
```

**Quick setup:** Run the interactive setup script to choose your deployment configuration:

```bash
./setup.py
```

Or directly specify the deployment type: `./setup.py local`, `./setup.py staging`, or `./setup.py production`.

The script copies the right files for your deployment scenario. Here's what each option sets up:

### Option 1: Local development with Uvicorn

Best for: **Development and testing**

**Copies:**

- `scripts/local_with_uvicorn/Dockerfile` → `Dockerfile`
- `scripts/local_with_uvicorn/docker-compose.yml` → `docker-compose.yml`
- `scripts/local_with_uvicorn/.env.example` → `src/.env`

Sets up Uvicorn with auto-reload enabled. The example environment values work fine for development.

**Manual setup:** `./setup.py local` or copy the files above manually.

### Option 2: Staging with Gunicorn managing Uvicorn workers

Best for: **Staging environments and load testing**

**Copies:**
## Quickstart

- `scripts/gunicorn_managing_uvicorn_workers/Dockerfile` → `Dockerfile`
- `scripts/gunicorn_managing_uvicorn_workers/docker-compose.yml` → `docker-compose.yml`
- `scripts/gunicorn_managing_uvicorn_workers/.env.example` → `src/.env`
1. Create your repository from this [template on GitHub](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template).

Sets up Gunicorn managing multiple Uvicorn workers for production-like performance testing.
1. Clone your repository.

> [!WARNING]
> Change `SECRET_KEY` and passwords in the `.env` file for staging environments.
1. Deploy it locally

**Manual setup:** `./setup.py staging` or copy the files above manually.
```
docker compose up
```

### Option 3: Production with NGINX

Best for: **Production deployments**

**Copies:**

- `scripts/production_with_nginx/Dockerfile` → `Dockerfile`
- `scripts/production_with_nginx/docker-compose.yml` → `docker-compose.yml`
- `scripts/production_with_nginx/.env.example` → `src/.env`

Sets up NGINX as reverse proxy with Gunicorn + Uvicorn workers for production.

> [!CAUTION]
> You MUST change `SECRET_KEY`, all passwords, and sensitive values in the `.env` file before deploying!

**Manual setup:** `./setup.py production` or copy the files above manually.

---

**Start your application:**

```bash
docker compose up
```

**Access your app:**
- **Local**: http://127.0.0.1:8000 (auto-reload enabled) → [API docs](http://127.0.0.1:8000/docs)
- **Staging**: http://127.0.0.1:8000 (production-like performance)
- **Production**: http://localhost (NGINX reverse proxy)
1. Open the API Swagger `http://localhost:8000/docs`

### Next steps

**Create your first admin user:**

```bash
docker compose run --rm create_superuser
```

**Run database migrations** (if you add models):

```bash
cd src && uv run alembic revision --autogenerate && uv run alembic upgrade head
```

**Test background jobs:**
```bash
curl -X POST 'http://127.0.0.1:8000/api/v1/tasks/task?message=hello'
```

**Or run locally without Docker:**
```bash
uv sync && uv run uvicorn src.app.main:app --reload
curl -X POST 'http://127.0.0.1:8000/api/v1/tasks/task?message=hello'
```

> Full setup (from-scratch, .env examples, PostgreSQL & Redis, gunicorn, nginx) lives in the [docs](https://benavlabs.github.io/FastAPI-boilerplate/getting-started/installation/).
Expand All @@ -177,8 +111,8 @@ Create `src/.env` and set **app**, **database**, **JWT**, and **environment** se

[https://benavlabs.github.io/FastAPI-boilerplate/getting-started/configuration/](https://benavlabs.github.io/FastAPI-boilerplate/getting-started/configuration/)

* `ENVIRONMENT=local|staging|production` controls API docs exposure
* Set `ADMIN_*` to enable the first admin user
- `ENVIRONMENT=local|staging|production` controls API docs exposure
- Set `ADMIN_*` to enable the first admin user

## Common tasks

Expand Down
32 changes: 0 additions & 32 deletions default.conf

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ services:
expose:
- "6379"

#-------- uncomment to run with nginx --------
# nginx:
# image: nginx:latest
# ports:
# - "80:80"
# volumes:
# - ./default.conf:/etc/nginx/conf.d/default.conf
# depends_on:
# - web

#-------- uncomment to create first superuser --------
create_superuser:
build:
Expand Down
1 change: 0 additions & 1 deletion docs/user-guide/configuration/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ services:
db: # PostgreSQL database
redis: # Redis for caching/queues
worker: # ARQ background task worker
nginx: # Reverse proxy (optional)
```

## Python Settings Classes
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ dependencies = [
"psycopg2-binary>=2.9.9",
"fastcrud>=0.19.2",
"crudadmin>=0.4.2",
"gunicorn>=23.0.0",
"ruff>=0.11.13",
"mypy>=1.16.0",
]
Expand Down
67 changes: 0 additions & 67 deletions scripts/gunicorn_managing_uvicorn_workers/.env.example

This file was deleted.

27 changes: 0 additions & 27 deletions scripts/gunicorn_managing_uvicorn_workers/Dockerfile

This file was deleted.

Loading