changed postgres_url to db, user, password etc

This commit is contained in:
Aaro Koivukangas
2025-10-16 15:29:18 +03:00
parent 774bb5f453
commit 21140d3477
2 changed files with 40 additions and 6 deletions
+25 -5
View File
@@ -1,4 +1,3 @@
version: "2"
services: services:
flask: flask:
build: build:
@@ -7,12 +6,33 @@ services:
# command: sh -c "gunicorn -w 4 flaskr:app -b 0.0.0.0:5000 --access-logfile '-' --error-logfile '-'" # command: sh -c "gunicorn -w 4 flaskr:app -b 0.0.0.0:5000 --access-logfile '-' --error-logfile '-'"
# command: sh -c "ls" # command: sh -c "ls"
ports: ports:
- "8200:5000" - "5000:5000"
volumes: volumes:
- .:/app - .:/app
env_file: .env env_file: .env
environment: environment:
- POSTGRES_URL - POSTGRES_DB
- POSTGRES_PORT
- POSTGRES_HOST
- POSTGRES_USER
- POSTGRES_PASSWORD
# extra_hosts:
# - "host.docker.internal:host-gateway"
depends_on:
- db
extra_hosts: db:
- "host.docker.internal:host-gateway" image: postgres:16-alpine
volumes:
- ./data/:/var/lib/postgresql/data
environment:
- POSTGRES_DB=timetrials-db
- POSTGRES_PORT=5432
- POSTGRES_HOST=db
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
healthcheck:
test: pg_isready -U postgres -d timetrials-db
interval: 10s
timeout: 3s
retries: 3
+15 -1
View File
@@ -3,8 +3,22 @@ from flaskr import app
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from random import randint from random import randint
if not getenv("POSTGRES_URL") or (getenv("POSTGRES_URL") and not getenv("POSTGRES_URL").startswith("postgresql://")):
postgres_host = getenv("POSTGRES_HOST")
postgres_db = getenv("POSTGRES_DB")
postgres_port = getenv("POSTGRES_PORT")
postgres_user = getenv("POSTGRES_USER")
postgres_password = getenv("POSTGRES_PASSWORD")
postgres_variables = [postgres_host, postgres_db, postgres_port, postgres_user, postgres_password]
for var in postgres_variables:
if not var:
raise ValueError("All environment variables not set")
app.config["SQLALCHEMY_DATABASE_URI"] = f"postgresql://{postgres_user}:{postgres_password}@{postgres_host}:{postgres_port}/{postgres_db}"
else:
app.config["SQLALCHEMY_DATABASE_URI"] = getenv("POSTGRES_URL")
app.config["SQLALCHEMY_DATABASE_URI"] = getenv("POSTGRES_URL")
db = SQLAlchemy(app) db = SQLAlchemy(app)