Skip to content

Commit 67284ff

Browse files
Adding the rest of the scripts for running the boilerplate with Gunicorn and in prod settings. The ones that were on the Gist.
Signed-off-by: Carlos Andrés Planchón Prestes <[email protected]>
1 parent d8196a7 commit 67284ff

File tree

7 files changed

+391
-0
lines changed

7 files changed

+391
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Use the template on GitHub, create your repo, then:
2828
git clone https://github.com/<you>/FastAPI-boilerplate
2929
cd FastAPI-boilerplate
3030

31+
# In the scripts/ folder, you can find scripts to run FastAPI-Boilerplate locally, with uvicorn workers, and in production with nginx.
32+
# NOTE: For now, only local scripts are updated.
33+
3134
# Running locally with Uvicorn:
3235

3336
# Copy Dockerfile and Docker Compose files:
@@ -93,6 +96,8 @@ Not a fit if you need a monorepo microservices scaffold - see the docs for point
9396

9497
Create `src/.env` and set **app**, **database**, **JWT**, and **environment** settings. See the docs for a copy-pasteable example and production guidance.
9598

99+
[https://benavlabs.github.io/FastAPI-boilerplate/getting-started/configuration/](https://benavlabs.github.io/FastAPI-boilerplate/getting-started/configuration/)
100+
96101
* `ENVIRONMENT=local|staging|production` controls API docs exposure
97102
* Set `ADMIN_*` to enable the first admin user
98103

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# --------- requirements ---------
2+
3+
FROM python:3.11 as requirements-stage
4+
5+
WORKDIR /tmp
6+
7+
RUN pip install poetry
8+
9+
COPY ./pyproject.toml ./poetry.lock* /tmp/
10+
11+
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
12+
13+
14+
# --------- final image build ---------
15+
FROM python:3.11
16+
17+
WORKDIR /code
18+
19+
COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt
20+
21+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
22+
23+
COPY ./src/app /code/app
24+
25+
# -------- replace with comment to run with gunicorn --------
26+
# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
27+
CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker". "-b", "0.0.0.0:8000"]
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
version: '3.8'
2+
3+
services:
4+
web:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
# -------- Both of the following commands should be commented to run with nginx --------
9+
10+
# -------- replace with comment to run with gunicorn or just uvicorn --------
11+
# command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
12+
command: gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
13+
env_file:
14+
- ./src/.env
15+
# -------- replace with expose if you are using nginx --------
16+
ports:
17+
- "8000:8000"
18+
# expose:
19+
# - "8000"
20+
depends_on:
21+
- db
22+
- redis
23+
volumes:
24+
- ./src/app:/code/app
25+
- ./src/.env:/code/.env
26+
27+
worker:
28+
build:
29+
context: .
30+
dockerfile: Dockerfile
31+
command: arq app.core.worker.settings.WorkerSettings
32+
env_file:
33+
- ./src/.env
34+
depends_on:
35+
- db
36+
- redis
37+
volumes:
38+
- ./src/app:/code/app
39+
- ./src/.env:/code/.env
40+
41+
db:
42+
image: postgres:13
43+
env_file:
44+
- ./src/.env
45+
volumes:
46+
- postgres-data:/var/lib/postgresql/data
47+
expose:
48+
- "5432"
49+
50+
redis:
51+
image: redis:alpine
52+
volumes:
53+
- redis-data:/data
54+
expose:
55+
- "6379"
56+
57+
#-------- uncomment to run with nginx --------
58+
# nginx:
59+
# image: nginx:latest
60+
# ports:
61+
# - "80:80"
62+
# volumes:
63+
# - ./default.conf:/etc/nginx/conf.d/default.conf
64+
# depends_on:
65+
# - web
66+
67+
#-------- uncomment to create first superuser --------
68+
create_superuser:
69+
build:
70+
context: .
71+
dockerfile: Dockerfile
72+
env_file:
73+
- ./src/.env
74+
depends_on:
75+
- db
76+
- web
77+
command: python -m src.scripts.create_first_superuser
78+
volumes:
79+
- ./src:/code/src
80+
81+
#-------- uncomment to run tests --------
82+
# pytest:
83+
# build:
84+
# context: .
85+
# dockerfile: Dockerfile
86+
# env_file:
87+
# - ./src/.env
88+
# depends_on:
89+
# - db
90+
# - create_superuser
91+
# - redis
92+
# command: python -m pytest ./tests
93+
# volumes:
94+
# - .:/code
95+
96+
#-------- uncomment to create first tier --------
97+
# create_tier:
98+
# build:
99+
# context: .
100+
# dockerfile: Dockerfile
101+
# env_file:
102+
# - ./src/.env
103+
# depends_on:
104+
# - create_superuser
105+
# - db
106+
# - web
107+
# command: python -m src.scripts.create_first_tier
108+
# volumes:
109+
# - ./src:/code/src
110+
111+
volumes:
112+
postgres-data:
113+
redis-data:
114+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# ------------- app settings -------------
2+
APP_NAME="My Project"
3+
APP_DESCRIPTION="My Project Description"
4+
APP_VERSION="0.1"
5+
CONTACT_NAME="Me"
6+
CONTACT_EMAIL="[email protected]"
7+
LICENSE_NAME="MIT"
8+
9+
# ------------- database -------------
10+
POSTGRES_USER="postgres"
11+
POSTGRES_PASSWORD=1234
12+
POSTGRES_SERVER="db"
13+
POSTGRES_PORT=5432
14+
POSTGRES_DB="postgres"
15+
POSTGRES_ASYNC_PREFIX="postgresql+asyncpg://"
16+
17+
# ------------- crypt -------------
18+
SECRET_KEY=953843cd400d99a039698e7feb46ca1b3e33c44fee2c24c6d88cf0f0b290fb61
19+
ALGORITHM=HS256
20+
ACCESS_TOKEN_EXPIRE_MINUTES=60
21+
22+
# ------------- admin -------------
23+
ADMIN_NAME="admin"
24+
ADMIN_EMAIL="[email protected]"
25+
ADMIN_USERNAME="admin"
26+
ADMIN_PASSWORD="Str1ngst!"
27+
28+
# ------------- redis cache -------------
29+
REDIS_CACHE_HOST="redis"
30+
REDIS_CACHE_PORT=6379
31+
32+
# ------------- redis queue -------------
33+
REDIS_QUEUE_HOST="redis"
34+
REDIS_QUEUE_PORT=6379
35+
36+
# ------------- redis rate limit -------------
37+
REDIS_RATE_LIMIT_HOST="redis"
38+
REDIS_RATE_LIMIT_PORT=6379
39+
40+
# ------------- client side cache -------------
41+
CLIENT_CACHE_MAX_AGE=60
42+
43+
# ------------- test -------------
44+
TEST_NAME="Tester User"
45+
TEST_EMAIL="[email protected]"
46+
TEST_USERNAME="testeruser"
47+
TEST_PASSWORD="Str1ng$t"
48+
49+
# ------------- environment -------------
50+
ENVIRONMENT="staging"
51+
52+
# ------------- first tier -------------
53+
TIER_NAME="free"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# --------- requirements ---------
2+
3+
FROM python:3.11 as requirements-stage
4+
5+
WORKDIR /tmp
6+
7+
RUN pip install poetry
8+
9+
COPY ./pyproject.toml ./poetry.lock* /tmp/
10+
11+
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
12+
13+
14+
# --------- final image build ---------
15+
FROM python:3.11
16+
17+
WORKDIR /code
18+
19+
COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt
20+
21+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
22+
23+
COPY ./src/app /code/app
24+
25+
# -------- replace with comment to run with gunicorn --------
26+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
27+
# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker". "-b", "0.0.0.0:8000"]
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
version: '3.8'
2+
3+
services:
4+
web:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
# -------- Both of the following commands should be commented to run with nginx --------
9+
10+
# -------- replace with comment to run with gunicorn --------
11+
# command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
12+
# command: gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
13+
env_file:
14+
- ./src/.env
15+
# -------- replace ports with expose if you are using nginx --------
16+
# ports:
17+
# - "8000:8000"
18+
expose:
19+
- "8000"
20+
depends_on:
21+
- db
22+
- redis
23+
volumes:
24+
- ./src/app:/code/app
25+
- ./src/.env:/code/.env
26+
27+
worker:
28+
build:
29+
context: .
30+
dockerfile: Dockerfile
31+
command: arq app.core.worker.settings.WorkerSettings
32+
env_file:
33+
- ./src/.env
34+
depends_on:
35+
- db
36+
- redis
37+
volumes:
38+
- ./src/app:/code/app
39+
- ./src/.env:/code/.env
40+
41+
db:
42+
image: postgres:13
43+
env_file:
44+
- ./src/.env
45+
volumes:
46+
- postgres-data:/var/lib/postgresql/data
47+
expose:
48+
- "5432"
49+
50+
redis:
51+
image: redis:alpine
52+
volumes:
53+
- redis-data:/data
54+
expose:
55+
- "6379"
56+
57+
#-------- uncomment to run with nginx --------
58+
nginx:
59+
image: nginx:latest
60+
ports:
61+
- "80:80"
62+
volumes:
63+
- ./default.conf:/etc/nginx/conf.d/default.conf
64+
depends_on:
65+
- web
66+
67+
#-------- uncomment to create first superuser --------
68+
# create_superuser:
69+
# build:
70+
# context: .
71+
# dockerfile: Dockerfile
72+
# env_file:
73+
# - ./src/.env
74+
# depends_on:
75+
# - db
76+
# - web
77+
# command: python -m src.scripts.create_first_superuser
78+
# volumes:
79+
# - ./src:/code/src
80+
81+
#-------- uncomment to run tests --------
82+
# pytest:
83+
# build:
84+
# context: .
85+
# dockerfile: Dockerfile
86+
# env_file:
87+
# - ./src/.env
88+
# depends_on:
89+
# - web
90+
# - redis
91+
# command: python -m pytest ./tests
92+
# volumes:
93+
# - .:/code
94+
95+
#-------- uncomment to create first tier --------
96+
# create_tier:
97+
# build:
98+
# context: .
99+
# dockerfile: Dockerfile
100+
# env_file:
101+
# - ./src/.env
102+
# depends_on:
103+
# - create_superuser
104+
# - db
105+
# - web
106+
# command: python -m src.scripts.create_first_tier
107+
# volumes:
108+
# - ./src:/code/src
109+
110+
volumes:
111+
postgres-data:
112+
redis-data:

0 commit comments

Comments
 (0)