Skip to content

Commit 90974b4

Browse files
committed
♻️ Refactor env vars handling
1 parent 69304ac commit 90974b4

4 files changed

Lines changed: 16 additions & 16 deletions

File tree

‎.env‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,5 @@ SMTP_TLS=False
1414
SMTP_PORT=1025
1515

1616
# Postgres
17-
POSTGRES_SERVER=localhost
18-
POSTGRES_DB=app
19-
POSTGRES_USER=postgres
2017
POSTGRES_PASSWORD=changethis
21-
22-
# Database URL for the backend (uses localhost for local dev, overridden in compose.yml for Docker)
23-
DATABASE_URL=postgresql+psycopg://postgres:changethis@localhost:5432/app
18+
DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@localhost:5432/app

‎backend/app/core/config.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
HttpUrl,
77
PostgresDsn,
88
computed_field,
9+
field_validator,
910
model_validator,
1011
)
1112
from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -29,6 +30,15 @@ class Settings(BaseSettings):
2930
SENTRY_DSN: HttpUrl | None = None
3031
DATABASE_URL: PostgresDsn
3132

33+
@field_validator("DATABASE_URL", mode="before")
34+
@classmethod
35+
def _use_psycopg_driver(cls, value: str | PostgresDsn) -> str:
36+
database_url = str(value)
37+
for scheme in ("postgres://", "postgresql://"):
38+
if database_url.startswith(scheme):
39+
return database_url.replace(scheme, "postgresql+psycopg://", 1)
40+
return database_url
41+
3242
SMTP_TLS: bool = True
3343
SMTP_SSL: bool = False
3444
SMTP_PORT: int = 587
@@ -69,6 +79,8 @@ def _check_default_secret(self, var_name: str, value: str | None) -> None:
6979
@model_validator(mode="after")
7080
def _enforce_non_default_secrets(self) -> Self:
7181
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
82+
for host in self.DATABASE_URL.hosts():
83+
self._check_default_secret("DATABASE_URL password", host["password"])
7284
self._check_default_secret(
7385
"FIRST_SUPERUSER_PASSWORD", self.FIRST_SUPERUSER_PASSWORD
7486
)

‎compose.yml‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ services:
2020
db:
2121
image: postgres:18
2222
healthcheck:
23-
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
23+
test: ["CMD-SHELL", "pg_isready -U postgres -d app"]
2424
interval: 10s
2525
retries: 5
2626
start_period: 30s
@@ -29,8 +29,7 @@ services:
2929
- app-db-data:/var/lib/postgresql
3030
environment:
3131
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:?Variable not set}
32-
- POSTGRES_USER=${POSTGRES_USER:?Variable not set}
33-
- POSTGRES_DB=${POSTGRES_DB:?Variable not set}
32+
- POSTGRES_DB=app
3433

3534
adminer:
3635
image: adminer
@@ -62,11 +61,7 @@ services:
6261
SMTP_USER: ${SMTP_USER:-}
6362
SMTP_PASSWORD: ${SMTP_PASSWORD:-}
6463
EMAILS_FROM_EMAIL: ${EMAILS_FROM_EMAIL}
65-
POSTGRES_SERVER: db
66-
POSTGRES_DB: ${POSTGRES_DB:?Variable not set}
67-
POSTGRES_USER: ${POSTGRES_USER:?Variable not set}
68-
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Variable not set}
69-
DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:?Variable not set}:${POSTGRES_PASSWORD:?Variable not set}@db:${POSTGRES_PORT:-5432}/${POSTGRES_DB}
64+
DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD:?Variable not set}@db:5432/app
7065
SENTRY_DSN: ${SENTRY_DSN:-}
7166

7267
healthcheck:

‎deployment.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ You can set several other environment variables:
8080
* `SMTP_USER`: The SMTP server user to send emails.
8181
* `SMTP_PASSWORD`: The SMTP server password to send emails.
8282
* `EMAILS_FROM_EMAIL`: The email account to send emails from.
83-
* `POSTGRES_USER`: The Postgres user, you can leave the default.
84-
* `POSTGRES_DB`: The database name to use for this application. You can leave the default of `app`.
8583
* `SENTRY_DSN`: The DSN for Sentry, if you are using it.
8684

8785
## GitHub Actions Environment Variables

0 commit comments

Comments
 (0)