Skip to content

Commit 4db9abe

Browse files
authored
Merge pull request #133 from igorbenav/igorbenav-patch-1
Update README.md
2 parents 5cb1898 + b4625b8 commit 4db9abe

File tree

1 file changed

+119
-23
lines changed

1 file changed

+119
-23
lines changed

README.md

Lines changed: 119 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
1. [JWT Authentication](#512-jwt-authentication)
100100
1. [Running](#513-running)
101101
1. [Create Application](#514-create-application)
102+
2. [Opting Out of Services](#515-opting-out-of-services)
102103
1. [Running in Production](#6-running-in-production)
103104
1. [Uvicorn Workers with Gunicorn](#61-uvicorn-workers-with-gunicorn)
104105
1. [Running With NGINX](#62-running-with-nginx)
@@ -1475,6 +1476,124 @@ And for the worker:
14751476
```sh
14761477
poetry run arq src.app.core.worker.settings.WorkerSettings
14771478
```
1479+
### 5.14 Create Application
1480+
1481+
If you want to stop tables from being created every time you run the api, you should disable this here:
1482+
1483+
```python
1484+
# app/main.py
1485+
1486+
from .api import router
1487+
from .core.config import settings
1488+
from .core.setup import create_application
1489+
1490+
# create_tables_on_start defaults to True
1491+
app = create_application(router=router, settings=settings, create_tables_on_start=False)
1492+
```
1493+
1494+
This `create_application` function is defined in `app/core/setup.py`, and it's a flexible way to configure the behavior of your application.
1495+
1496+
A few examples:
1497+
1498+
- Deactivate or password protect /docs
1499+
- Add client-side cache middleware
1500+
- Add Startup and Shutdown event handlers for cache, queue and rate limit
1501+
1502+
### 5.15 Opting Out of Services
1503+
1504+
To opt out of services (like `Redis`, `Queue`, `Rate Limiter`), head to the `Settings` class in `src/app/core/config`:
1505+
1506+
```python
1507+
# src/app/core/config
1508+
import os
1509+
from enum import Enum
1510+
1511+
from pydantic_settings import BaseSettings
1512+
from starlette.config import Config
1513+
1514+
current_file_dir = os.path.dirname(os.path.realpath(__file__))
1515+
env_path = os.path.join(current_file_dir, "..", "..", ".env")
1516+
config = Config(env_path)
1517+
...
1518+
1519+
class Settings(
1520+
AppSettings,
1521+
PostgresSettings,
1522+
CryptSettings,
1523+
FirstUserSettings,
1524+
TestSettings,
1525+
RedisCacheSettings,
1526+
ClientSideCacheSettings,
1527+
RedisQueueSettings,
1528+
RedisRateLimiterSettings,
1529+
DefaultRateLimitSettings,
1530+
EnvironmentSettings,
1531+
):
1532+
pass
1533+
1534+
1535+
settings = Settings()
1536+
```
1537+
1538+
And remove the Settings of the services you do not need. For example, without using redis (removed `Cache`, `Queue` and `Rate limit`):
1539+
1540+
```python
1541+
class Settings(
1542+
AppSettings,
1543+
PostgresSettings,
1544+
CryptSettings,
1545+
FirstUserSettings,
1546+
TestSettings,
1547+
ClientSideCacheSettings,
1548+
DefaultRateLimitSettings,
1549+
EnvironmentSettings,
1550+
):
1551+
pass
1552+
```
1553+
1554+
Then comment or remove the services you do not want from `docker-compose.yml`. Here, I removed `redis` and `worker` services:
1555+
1556+
```yml
1557+
version: '3.8'
1558+
1559+
services:
1560+
web:
1561+
build:
1562+
context: .
1563+
dockerfile: Dockerfile
1564+
# -------- replace with comment to run with gunicorn --------
1565+
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
1566+
# command: gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
1567+
env_file:
1568+
- ./src/.env
1569+
# -------- replace with comment if you are using nginx --------
1570+
ports:
1571+
- "8000:8000"
1572+
# expose:
1573+
# - "8000"
1574+
depends_on:
1575+
- db
1576+
- redis
1577+
volumes:
1578+
- ./src/app:/code/app
1579+
- ./src/.env:/code/.env
1580+
db:
1581+
image: postgres:13
1582+
env_file:
1583+
- ./src/.env
1584+
volumes:
1585+
- postgres-data:/var/lib/postgresql/data
1586+
# -------- replace with comment to run migrations with docker --------
1587+
expose:
1588+
- "5432"
1589+
# ports:
1590+
# - 5432:5432
1591+
1592+
volumes:
1593+
postgres-data:
1594+
redis-data:
1595+
#pgadmin-data:
1596+
```
14781597

14791598
## 6. Running in Production
14801599

@@ -1531,29 +1650,6 @@ CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker
15311650
> \[!CAUTION\]
15321651
> Do not forget to set the `ENVIRONMENT` in `.env` to `production` unless you want the API docs to be public.
15331652

1534-
### 5.14 Create Application
1535-
1536-
If you want to stop tables from being created every time you run the api, you should disable this here:
1537-
1538-
```python
1539-
# app/main.py
1540-
1541-
from .api import router
1542-
from .core.config import settings
1543-
from .core.setup import create_application
1544-
1545-
# create_tables_on_start defaults to True
1546-
app = create_application(router=router, settings=settings, create_tables_on_start=False)
1547-
```
1548-
1549-
This `create_application` function is defined in `app/core/setup.py`, and it's a flexible way to configure the behavior of your application.
1550-
1551-
A few examples:
1552-
1553-
- Deactivate or password protect /docs
1554-
- Add client-side cache middleware
1555-
- Add Startup and Shutdown event handlers for cache, queue and rate limit
1556-
15571653
### 6.2 Running with NGINX
15581654

15591655
NGINX is a high-performance web server, known for its stability, rich feature set, simple configuration, and low resource consumption. NGINX acts as a reverse proxy, that is, it receives client requests, forwards them to the FastAPI server (running via Uvicorn or Gunicorn), and then passes the responses back to the clients.

0 commit comments

Comments
 (0)