Skip to content

Commit 2df955a

Browse files
authored
initial documentation (#177)
1 parent e2d299b commit 2df955a

39 files changed

+14998
-5
lines changed

README.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<h1 align="center"> Fast FastAPI boilerplate</h1>
1+
<h1 align="center"> Benav Labs FastAPI boilerplate</h1>
22
<p align="center" markdown=1>
33
<i>Yet another template to speed your FastAPI development up.</i>
44
</p>
55

66
<p align="center">
77
<a href="https://github.com/igormagalhaesr/FastAPI-boilerplate">
8-
<img src="https://user-images.githubusercontent.com/43156212/277095260-ef5d4496-8290-4b18-99b2-0c0b5500504e.png" alt="Blue Rocket with FastAPI Logo as its window. There is a word FAST written" width="35%" height="auto">
8+
<img src="docs/assets/FastAPI-boilerplate.png" alt="Purple Rocket with FastAPI Logo as its window." width="35%" height="auto">
99
</a>
1010
</p>
1111

@@ -33,6 +33,16 @@
3333
</a>
3434
</p>
3535

36+
---
37+
38+
## 📖 Documentation
39+
40+
📚 **[Visit our comprehensive documentation at benavlabs.github.io/fastapi-boilerplate](https://benavlabs.github.io/fastapi-boilerplate/)**
41+
42+
This README provides a quick reference for LLMs and developers, but the full documentation contains detailed guides, examples, and best practices.
43+
44+
---
45+
3646
## 0. About
3747

3848
**FastAPI boilerplate** creates an extendable async API using FastAPI, Pydantic V2, SQLAlchemy 2.0 and PostgreSQL:
@@ -47,7 +57,7 @@
4757
- [`NGINX`](https://nginx.org/en/) High-performance low resource consumption web server used for Reverse Proxy and Load Balancing.
4858

4959
> \[!TIP\]
50-
> If you want the `SQLModel` version instead, head to [SQLModel-boilerplate](https://github.com/igorbenav/SQLModel-boilerplate).
60+
> There's a `SQLModel` version as well, but it's no longer updated: [SQLModel-boilerplate](https://github.com/igorbenav/SQLModel-boilerplate).
5161
5262
## 1. Features
5363

@@ -62,8 +72,6 @@
6272
- ⎘ Out of the box offset and cursor pagination support with <a href="https://github.com/igorbenav/fastcrud">fastcrud</a>
6373
- 🛑 Rate Limiter dependency
6474
- 👮 FastAPI docs behind authentication and hidden based on the environment
65-
- 🦾 Easily extendable
66-
- 🤸‍♂️ Flexible
6775
- 🚚 Easy running with docker compose
6876
- ⚖️ NGINX Reverse Proxy and Load Balancing
6977

@@ -118,6 +126,8 @@ ______________________________________________________________________
118126

119127
## 3. Prerequisites
120128

129+
> 📖 **[See detailed installation guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/getting-started/installation/)**
130+
121131
### 3.0 Start
122132

123133
Start by using the template, and naming the repository to what you want.
@@ -144,6 +154,8 @@ git clone https://github.com/igormagalhaesr/FastAPI-boilerplate
144154
145155
### 3.1 Environment Variables (.env)
146156

157+
> 📖 **[See complete configuration guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/getting-started/configuration/)**
158+
147159
Then create a `.env` file inside `src` directory:
148160

149161
```sh
@@ -302,6 +314,8 @@ pip install uv
302314

303315
## 4. Usage
304316

317+
> 📖 **[See complete first run guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/getting-started/first-run/)**
318+
305319
### 4.1 Docker Compose
306320

307321
If you used docker compose, your setup is done. You just need to ensure that when you run (while in the base folder):
@@ -530,8 +544,12 @@ uv run alembic upgrade head
530544
531545
## 5. Extending
532546

547+
> 📖 **[See comprehensive development guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/development/)**
548+
533549
### 5.1 Project Structure
534550

551+
> 📖 **[See detailed project structure guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/project-structure/)**
552+
535553
First, you may want to take a look at the project structure and understand what each file is doing.
536554

537555
```sh
@@ -661,6 +679,8 @@ Note that this table is used to blacklist the `JWT` tokens (it's how you log a u
661679

662680
### 5.3 SQLAlchemy Models
663681

682+
> 📖 **[See database models guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/database/models/)**
683+
664684
Inside `app/models`, create a new `entity.py` for each new entity (replacing entity with the name) and define the attributes according to [SQLAlchemy 2.0 standards](https://docs.sqlalchemy.org/en/20/orm/mapping_styles.html#orm-mapping-styles):
665685

666686
> \[!WARNING\]
@@ -683,6 +703,8 @@ class Entity(Base):
683703

684704
### 5.4 Pydantic Schemas
685705

706+
> 📖 **[See database schemas guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/database/schemas/)**
707+
686708
Inside `app/schemas`, create a new `entity.py` for each new entity (replacing entity with the name) and create the schemas according to [Pydantic V2](https://docs.pydantic.dev/latest/#pydantic-examples) standards:
687709

688710
```python
@@ -731,6 +753,8 @@ class EntityDelete(BaseModel):
731753

732754
### 5.5 Alembic Migrations
733755

756+
> 📖 **[See database migrations guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/database/migrations/)**
757+
734758
> \[!WARNING\]
735759
> To create the tables if you did not create the endpoints, ensure that you import the models in src/app/models/__init__.py. This step is crucial to create the new models.
736760
@@ -748,6 +772,8 @@ uv run alembic upgrade head
748772

749773
### 5.6 CRUD
750774

775+
> 📖 **[See CRUD operations guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/database/crud/)**
776+
751777
Inside `app/crud`, create a new `crud_entities.py` inheriting from `FastCRUD` for each new entity:
752778

753779
```python
@@ -953,6 +979,8 @@ crud_user.get(db=db, username="myusername", schema_to_select=UserRead)
953979

954980
### 5.7 Routes
955981

982+
> 📖 **[See API endpoints guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/api/endpoints/)**
983+
956984
Inside `app/api/v1`, create a new `entities.py` file and create the desired routes
957985

958986
```python
@@ -993,6 +1021,8 @@ router.include_router(entity_router)
9931021

9941022
#### 5.7.1 Paginated Responses
9951023

1024+
> 📖 **[See API pagination guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/api/pagination/)**
1025+
9961026
With the `get_multi` method we get a python `dict` with full suport for pagination:
9971027

9981028
```javascript
@@ -1057,6 +1087,8 @@ async def read_entities(
10571087

10581088
#### 5.7.2 HTTP Exceptions
10591089

1090+
> 📖 **[See API exceptions guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/api/exceptions/)**
1091+
10601092
To add exceptions you may just import from `app/core/exceptions/http_exceptions` and optionally add a detail:
10611093

10621094
```python
@@ -1084,6 +1116,8 @@ if not post:
10841116

10851117
### 5.8 Caching
10861118

1119+
> 📖 **[See comprehensive caching guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/caching/)**
1120+
10871121
The `cache` decorator allows you to cache the results of FastAPI endpoint functions, enhancing response times and reducing the load on your application by storing and retrieving data in a cache.
10881122

10891123
Caching the response of an endpoint is really simple, just apply the `cache` decorator to the endpoint function.
@@ -1247,6 +1281,8 @@ For `client-side caching`, all you have to do is let the `Settings` class define
12471281

12481282
### 5.10 ARQ Job Queues
12491283

1284+
> 📖 **[See background tasks guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/background-tasks/)**
1285+
12501286
Depending on the problem your API is solving, you might want to implement a job queue. A job queue allows you to run tasks in the background, and is usually aimed at functions that require longer run times and don't directly impact user response in your frontend. As a rule of thumb, if a task takes more than 2 seconds to run, can be executed asynchronously, and its result is not needed for the next step of the user's interaction, then it is a good candidate for the job queue.
12511287

12521288
> [!TIP]
@@ -1342,6 +1378,8 @@ async def your_background_function(
13421378
13431379
### 5.11 Rate Limiting
13441380

1381+
> 📖 **[See rate limiting guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/rate-limiting/)**
1382+
13451383
To limit how many times a user can make a request in a certain interval of time (very useful to create subscription plans or just to protect your API against DDOS), you may just use the `rate_limiter_dependency` dependency:
13461384

13471385
```python
@@ -1456,6 +1494,8 @@ Note that for flexibility (since this is a boilerplate), it's not necessary to p
14561494
14571495
### 5.12 JWT Authentication
14581496

1497+
> 📖 **[See authentication guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/authentication/)**
1498+
14591499
#### 5.12.1 Details
14601500

14611501
The JWT in this boilerplate is created in the following way:
@@ -1641,6 +1681,8 @@ volumes:
16411681

16421682
## 6. Running in Production
16431683

1684+
> 📖 **[See production deployment guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/production/)**
1685+
16441686
### 6.1 Uvicorn Workers with Gunicorn
16451687

16461688
In production you may want to run using gunicorn to manage uvicorn workers:
@@ -1834,6 +1876,8 @@ And finally, on your browser: `http://localhost/docs`.
18341876

18351877
## 7. Testing
18361878

1879+
> 📖 **[See comprehensive testing guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/testing/)**
1880+
18371881
This project uses **fast unit tests** that don't require external services like databases or Redis. Tests are isolated using mocks and run in milliseconds.
18381882

18391883
### 7.1 Writing Tests
390 KB
Loading
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Configuration
2+
3+
This guide covers the essential configuration steps to get your FastAPI application running quickly.
4+
5+
## Quick Setup
6+
7+
The fastest way to get started is to copy the example environment file and modify just a few values:
8+
9+
```bash
10+
cp src/.env.example src/.env
11+
```
12+
13+
## Essential Configuration
14+
15+
Open `src/.env` and set these required values:
16+
17+
### Application Settings
18+
19+
```env
20+
# App Settings
21+
APP_NAME="Your app name here"
22+
APP_DESCRIPTION="Your app description here"
23+
APP_VERSION="0.1"
24+
CONTACT_NAME="Your name"
25+
CONTACT_EMAIL="Your email"
26+
LICENSE_NAME="The license you picked"
27+
```
28+
29+
### Database Connection
30+
31+
```env
32+
# Database
33+
POSTGRES_USER="your_postgres_user"
34+
POSTGRES_PASSWORD="your_password"
35+
POSTGRES_SERVER="localhost" # Use "db" for Docker Compose
36+
POSTGRES_PORT=5432 # Use 5432 for Docker Compose
37+
POSTGRES_DB="your_database_name"
38+
```
39+
40+
### PGAdmin (Optional)
41+
42+
For database administration:
43+
44+
```env
45+
# PGAdmin
46+
PGADMIN_DEFAULT_EMAIL="your_email_address"
47+
PGADMIN_DEFAULT_PASSWORD="your_password"
48+
PGADMIN_LISTEN_PORT=80
49+
```
50+
51+
**To connect to database in PGAdmin:**
52+
1. Login with `PGADMIN_DEFAULT_EMAIL` and `PGADMIN_DEFAULT_PASSWORD`
53+
2. Click "Add Server"
54+
3. Use these connection settings:
55+
- **Hostname/address**: `db` (if using containers) or `localhost`
56+
- **Port**: Value from `POSTGRES_PORT`
57+
- **Database**: `postgres` (leave as default)
58+
- **Username**: Value from `POSTGRES_USER`
59+
- **Password**: Value from `POSTGRES_PASSWORD`
60+
61+
### Security
62+
63+
Generate a secret key and set it:
64+
65+
```bash
66+
# Generate a secure secret key
67+
openssl rand -hex 32
68+
```
69+
70+
```env
71+
# Cryptography
72+
SECRET_KEY="your-generated-secret-key-here" # Result of openssl rand -hex 32
73+
ALGORITHM="HS256" # Default: HS256
74+
ACCESS_TOKEN_EXPIRE_MINUTES=30 # Default: 30
75+
REFRESH_TOKEN_EXPIRE_DAYS=7 # Default: 7
76+
```
77+
78+
### First Admin User
79+
80+
```env
81+
# Admin User
82+
ADMIN_NAME="your_name"
83+
ADMIN_EMAIL="your_email"
84+
ADMIN_USERNAME="your_username"
85+
ADMIN_PASSWORD="your_password"
86+
```
87+
88+
### Redis Configuration
89+
90+
```env
91+
# Redis Cache
92+
REDIS_CACHE_HOST="localhost" # Use "redis" for Docker Compose
93+
REDIS_CACHE_PORT=6379
94+
95+
# Client-side Cache
96+
CLIENT_CACHE_MAX_AGE=30 # Default: 30 seconds
97+
98+
# Redis Job Queue
99+
REDIS_QUEUE_HOST="localhost" # Use "redis" for Docker Compose
100+
REDIS_QUEUE_PORT=6379
101+
102+
# Redis Rate Limiting
103+
REDIS_RATE_LIMIT_HOST="localhost" # Use "redis" for Docker Compose
104+
REDIS_RATE_LIMIT_PORT=6379
105+
```
106+
107+
!!! warning "Redis in Production"
108+
You may use the same Redis instance for caching and queues while developing, but use separate containers in production.
109+
110+
### Rate Limiting Defaults
111+
112+
```env
113+
# Default Rate Limits
114+
DEFAULT_RATE_LIMIT_LIMIT=10 # Default: 10 requests
115+
DEFAULT_RATE_LIMIT_PERIOD=3600 # Default: 3600 seconds (1 hour)
116+
```
117+
118+
### First Tier
119+
120+
```env
121+
# Default Tier
122+
TIER_NAME="free"
123+
```
124+
125+
## Environment Types
126+
127+
Set your environment type:
128+
129+
```env
130+
ENVIRONMENT="local" # local, staging, or production
131+
```
132+
133+
- **local**: API docs available at `/docs`, `/redoc`, and `/openapi.json`
134+
- **staging**: API docs available to superusers only
135+
- **production**: API docs completely disabled
136+
137+
## Docker Compose Settings
138+
139+
If using Docker Compose, use these values instead:
140+
141+
```env
142+
# Docker Compose values
143+
POSTGRES_SERVER="db"
144+
REDIS_CACHE_HOST="redis"
145+
REDIS_QUEUE_HOST="redis"
146+
REDIS_RATE_LIMIT_HOST="redis"
147+
```
148+
149+
## Optional Services
150+
151+
The boilerplate includes Redis for caching, job queues, and rate limiting. If running locally without Docker, either:
152+
153+
1. **Install Redis** and keep the default settings
154+
2. **Disable Redis services** (see [User Guide - Configuration](../user-guide/configuration/index.md) for details)
155+
156+
## That's It!
157+
158+
With these basic settings configured, you can start the application:
159+
160+
- **Docker Compose**: `docker compose up`
161+
- **Manual**: `uv run uvicorn src.app.main:app --reload`
162+
163+
For detailed configuration options, advanced settings, and production deployment, see the [User Guide - Configuration](../user-guide/configuration/index.md).

0 commit comments

Comments
 (0)