Skip to content

Commit

Permalink
Merge pull request #4 from districtr/handle-missing-env-vars-2
Browse files Browse the repository at this point in the history
Simplify handling of postgres env vars
  • Loading branch information
raphaellaude authored Jul 20, 2024
2 parents 7646b12 + b8cd017 commit 938d9cd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
PROJECT_NAME: Districtr v2 backend
BACKEND_CORS_ORIGINS: "http://localhost,http://localhost:5173"
SECRET_KEY: mysupersecretkey
DATABASE_URL: postgresql+psycopg://postgres:postgres@postgres:5432/postgres
POSTGRES_SCHEME: postgresql+psycopg
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand Down
15 changes: 15 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
fly.toml
.venv/
.pytest_cache/
__pycache__/
*.pyc
.env
.ruff_cache/
scratch/
.git
*.whl
*.deb
*.rpm
*.tar.gz
*.zip
.pyenv/
*pyvenv*
26 changes: 7 additions & 19 deletions backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,18 @@ def server_host(self) -> str:
# Postgres

POSTGRES_SCHEME: str
POSTGRES_SERVER: str | None
POSTGRES_PORT: int | None = 5432
POSTGRES_USER: str | None
POSTGRES_PASSWORD: str | None
POSTGRES_DB: str = ""
DATABASE_URL: str | None = None
POSTGRES_SERVER: str
POSTGRES_PORT: int = 5432
POSTGRES_USER: str
POSTGRES_PASSWORD: str
POSTGRES_DB: str
DATABASE_URL: str

@computed_field # type: ignore[misc]
@property
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
if self.DATABASE_URL:
db_uri = MultiHostUrl(self.DATABASE_URL)
(host,) = db_uri.hosts()

self.POSTGRES_SCHEME = db_uri.scheme
self.POSTGRES_PORT = host["port"]
self.POSTGRES_USER = host["username"]
self.POSTGRES_PASSWORD = host["password"]
self.POSTGRES_SERVER = host["host"]

if db_uri.path:
self.POSTGRES_DB = db_uri.path.lstrip("/")

return db_uri
return MultiHostUrl(self.DATABASE_URL)

return MultiHostUrl.build(
scheme=self.POSTGRES_SCHEME,
Expand Down

0 comments on commit 938d9cd

Please sign in to comment.