Skip to content

Commit 9c8983e

Browse files
authored
♻️ Simplify environment configuration (#2413)
1 parent 2d04828 commit 9c8983e

6 files changed

Lines changed: 47 additions & 68 deletions

File tree

.env

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ DOMAIN=localhost
66
# DOMAIN=localhost.tiangolo.com
77

88
# Public frontend URL, used by the backend to generate links in emails.
9-
# Keep the Vite URL for local development; deployments use the application URL.
9+
# Deployments derive this from DOMAIN in compose.deploy.yml.
1010
FRONTEND_HOST=http://localhost:5173
11-
# In staging and production, set this env var to the frontend host, e.g.
12-
# FRONTEND_HOST=https://example.com
1311

1412
# Environment: local, staging, production
1513
ENVIRONMENT=local
1614

1715
PROJECT_NAME="Full Stack FastAPI Project"
1816

1917
# Backend
20-
BACKEND_CORS_ORIGINS="http://localhost,https://localhost,http://localhost.tiangolo.com"
18+
# Additional allowed CORS origins, separated by commas
19+
BACKEND_CORS_ORIGINS=
2120
SECRET_KEY=changethis
2221
FIRST_SUPERUSER=admin@example.com
2322
FIRST_SUPERUSER_PASSWORD=changethis
@@ -39,6 +38,3 @@ POSTGRES_USER=postgres
3938
POSTGRES_PASSWORD=changethis
4039

4140
SENTRY_DSN=
42-
43-
# Configure these with your own Docker registry images
44-
DOCKER_IMAGE_BACKEND=backend

.github/workflows/deploy-production.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ jobs:
1919
env:
2020
ENVIRONMENT: production
2121
DOMAIN: ${{ secrets.DOMAIN }}
22-
FRONTEND_HOST: https://${{ secrets.DOMAIN }}
2322
SECRET_KEY: ${{ secrets.SECRET_KEY }}
2423
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }}
2524
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }}

.github/workflows/deploy-staging.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ jobs:
1919
env:
2020
ENVIRONMENT: staging
2121
DOMAIN: ${{ secrets.DOMAIN }}
22-
FRONTEND_HOST: https://${{ secrets.DOMAIN }}
2322
SECRET_KEY: ${{ secrets.SECRET_KEY }}
2423
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }}
2524
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }}

compose.deploy.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ services:
4444
# Use the Let's Encrypt resolver
4545
- traefik.http.routers.adminer-https.tls.certresolver=le
4646

47+
prestart:
48+
environment:
49+
FRONTEND_HOST: https://${DOMAIN?Variable not set}
50+
4751
backend:
4852
restart: always
53+
environment:
54+
FRONTEND_HOST: https://${DOMAIN?Variable not set}
4955
labels:
5056
# Route HTTPS traffic for this domain
5157
- traefik.http.routers.backend-https.rule=Host(`${DOMAIN?Variable not set}`)

compose.yml

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -49,65 +49,36 @@ services:
4949
# Define the port inside of the Docker service to use
5050
- traefik.http.services.adminer.loadbalancer.server.port=8080
5151

52-
prestart:
53-
image: '${DOCKER_IMAGE_BACKEND?Variable not set}:${TAG-latest}'
54-
build:
55-
context: .
56-
dockerfile: backend/Dockerfile
57-
depends_on:
58-
db:
59-
condition: service_healthy
60-
restart: true
61-
command: bash scripts/prestart.sh
62-
env_file:
63-
- .env
64-
environment:
65-
- DOMAIN=${DOMAIN}
66-
- FRONTEND_HOST=${FRONTEND_HOST?Variable not set}
67-
- ENVIRONMENT=${ENVIRONMENT}
68-
- BACKEND_CORS_ORIGINS=${BACKEND_CORS_ORIGINS}
69-
- SECRET_KEY=${SECRET_KEY?Variable not set}
70-
- FIRST_SUPERUSER=${FIRST_SUPERUSER?Variable not set}
71-
- FIRST_SUPERUSER_PASSWORD=${FIRST_SUPERUSER_PASSWORD?Variable not set}
72-
- SMTP_HOST=${SMTP_HOST}
73-
- SMTP_USER=${SMTP_USER}
74-
- SMTP_PASSWORD=${SMTP_PASSWORD}
75-
- EMAILS_FROM_EMAIL=${EMAILS_FROM_EMAIL}
76-
- POSTGRES_SERVER=db
77-
- POSTGRES_PORT=${POSTGRES_PORT}
78-
- POSTGRES_DB=${POSTGRES_DB}
79-
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
80-
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
81-
- SENTRY_DSN=${SENTRY_DSN}
82-
8352
backend:
84-
image: '${DOCKER_IMAGE_BACKEND?Variable not set}:${TAG-latest}'
53+
image: backend:latest
8554
depends_on:
8655
db:
8756
condition: service_healthy
8857
restart: true
8958
prestart:
9059
condition: service_completed_successfully
91-
env_file:
92-
- .env
93-
environment:
94-
- DOMAIN=${DOMAIN}
95-
- FRONTEND_HOST=${FRONTEND_HOST?Variable not set}
96-
- ENVIRONMENT=${ENVIRONMENT}
97-
- BACKEND_CORS_ORIGINS=${BACKEND_CORS_ORIGINS}
98-
- SECRET_KEY=${SECRET_KEY?Variable not set}
99-
- FIRST_SUPERUSER=${FIRST_SUPERUSER?Variable not set}
100-
- FIRST_SUPERUSER_PASSWORD=${FIRST_SUPERUSER_PASSWORD?Variable not set}
101-
- SMTP_HOST=${SMTP_HOST}
102-
- SMTP_USER=${SMTP_USER}
103-
- SMTP_PASSWORD=${SMTP_PASSWORD}
104-
- EMAILS_FROM_EMAIL=${EMAILS_FROM_EMAIL}
105-
- POSTGRES_SERVER=db
106-
- POSTGRES_PORT=${POSTGRES_PORT}
107-
- POSTGRES_DB=${POSTGRES_DB}
108-
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
109-
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
110-
- SENTRY_DSN=${SENTRY_DSN}
60+
# The YAML anchor lets the prestart service reuse this environment mapping.
61+
environment: &backend-environment
62+
FRONTEND_HOST: ${FRONTEND_HOST?Variable not set}
63+
ENVIRONMENT: ${ENVIRONMENT}
64+
PROJECT_NAME: ${PROJECT_NAME?Variable not set}
65+
BACKEND_CORS_ORIGINS: ${BACKEND_CORS_ORIGINS}
66+
SECRET_KEY: ${SECRET_KEY?Variable not set}
67+
FIRST_SUPERUSER: ${FIRST_SUPERUSER?Variable not set}
68+
FIRST_SUPERUSER_PASSWORD: ${FIRST_SUPERUSER_PASSWORD?Variable not set}
69+
SMTP_HOST: ${SMTP_HOST}
70+
SMTP_USER: ${SMTP_USER}
71+
SMTP_PASSWORD: ${SMTP_PASSWORD}
72+
EMAILS_FROM_EMAIL: ${EMAILS_FROM_EMAIL}
73+
SMTP_TLS: ${SMTP_TLS}
74+
SMTP_SSL: ${SMTP_SSL}
75+
SMTP_PORT: ${SMTP_PORT}
76+
POSTGRES_SERVER: db
77+
POSTGRES_PORT: ${POSTGRES_PORT}
78+
POSTGRES_DB: ${POSTGRES_DB}
79+
POSTGRES_USER: ${POSTGRES_USER?Variable not set}
80+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD?Variable not set}
81+
SENTRY_DSN: ${SENTRY_DSN}
11182

11283
healthcheck:
11384
test: ["CMD", "curl", "-f", "http://localhost:8000/api/v1/utils/health-check/"]
@@ -129,5 +100,17 @@ services:
129100
- traefik.http.routers.backend-http.rule=Host(`${DOMAIN?Variable not set}`)
130101
- traefik.http.routers.backend-http.entrypoints=http
131102

103+
prestart:
104+
image: backend:latest
105+
build:
106+
context: .
107+
dockerfile: backend/Dockerfile
108+
depends_on:
109+
db:
110+
condition: service_healthy
111+
restart: true
112+
command: bash scripts/prestart.sh
113+
environment: *backend-environment
114+
132115
volumes:
133116
app-db-data:

deployment.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Set the `DOMAIN`, by default `localhost` (for development), but when deploying y
5858
export DOMAIN=fastapi-project.example.com
5959
```
6060

61+
The deployment Docker Compose configuration also uses `DOMAIN` to set the public frontend URL used in links generated by the backend.
62+
6163
Set the `POSTGRES_PASSWORD` to something different than `changethis`:
6264

6365
```bash
@@ -72,18 +74,12 @@ export SECRET_KEY="changethis"
7274

7375
Note: you can use the Python command above to generate a secure secret key.
7476

75-
Set the `FIRST_SUPER_USER_PASSWORD` to something different than `changethis`:
77+
Set the `FIRST_SUPERUSER_PASSWORD` to something different than `changethis`:
7678

7779
```bash
7880
export FIRST_SUPERUSER_PASSWORD="changethis"
7981
```
8082

81-
Set the `FRONTEND_HOST` to the application URL. It is used to generate links in emails:
82-
83-
```bash
84-
export FRONTEND_HOST="https://${DOMAIN?Variable not set}"
85-
```
86-
8783
You can set several other environment variables:
8884

8985
* `PROJECT_NAME`: The name of the project, used in the API for the docs and emails.

0 commit comments

Comments
 (0)