From ef2529d56eaa978c66739ad7625fd39b2206392f Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Thu, 26 Dec 2024 01:33:14 +1100 Subject: [PATCH] Use production config, and resolve issue with postgres url format (Unsure why this only surfaced after recreation of app) --- Dockerfile | 4 ++++ Readme.md | 2 +- app/__init__.py | 8 ++++++-- app/config.py | 6 +++--- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index e9fd5df..8f43904 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,5 +43,9 @@ COPY --from=tailwind-builder /app/app/static/css/tailwind.css ./app/static/css/t # Expose the port Flask will run on EXPOSE 5000 +# Set environment variables +ENV FLASK_ENV=production +ENV FLASK_CONFIG=ProductionConfig + # Command to run the Flask app CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:create_app()"] diff --git a/Readme.md b/Readme.md index c535b9c..ae62200 100644 --- a/Readme.md +++ b/Readme.md @@ -28,7 +28,7 @@ ### Run locally -`docker run -p 5000:5000 -e DATABASE_URL=postgresql://postgres:59fff56880e1bbb42e753d2a82ac21b6@peterstockings.com:15389/bloodpressure_db bloodpressure` +`docker run -p 5000:5000 -e DATABASE_URL=postgres://postgres:59fff56880e1bbb42e753d2a82ac21b6@peterstockings.com:15389/bloodpressure_db bloodpressure` # Fix deployment issues diff --git a/app/__init__.py b/app/__init__.py index 2f0dde8..7a99602 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,3 +1,4 @@ +import os from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate @@ -13,9 +14,12 @@ login_manager = LoginManager() login_manager.login_view = 'auth.login' login_manager.login_message_category = 'info' -def create_app(config_class='app.config.DevelopmentConfig'): +def create_app(): app = Flask(__name__) - app.config.from_object(config_class) + + # Load configuration + config_name = os.getenv('FLASK_CONFIG', 'DevelopmentConfig') # Default to DevelopmentConfig + app.config.from_object(f'app.config.{config_name}') # Initialize extensions db.init_app(app) diff --git a/app/config.py b/app/config.py index 0aafdad..68876b2 100644 --- a/app/config.py +++ b/app/config.py @@ -9,17 +9,17 @@ class DevelopmentConfig(Config): """Development configuration.""" DEBUG = True - uri = os.environ.get('DATABASE_URL', 'postgresql://postgres:59fff56880e1bbb42e753d2a82ac21b6@peterstockings.com:15389/bloodpressure_db') + uri = os.environ.get('DATABASE_URL', 'postgres://postgres:59fff56880e1bbb42e753d2a82ac21b6@peterstockings.com:15389/bloodpressure_db') if uri and uri.startswith("postgres://"): uri = uri.replace("postgres://", "postgresql://", 1) - SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'postgresql://postgres:59fff56880e1bbb42e753d2a82ac21b6@peterstockings.com:15389/bloodpressure_db') + SQLALCHEMY_DATABASE_URI = uri class ProductionConfig(Config): """Production configuration.""" DEBUG = False - uri = os.environ.get('DATABASE_URL', 'postgresql://postgres:59fff56880e1bbb42e753d2a82ac21b6@peterstockings.com:15389/bloodpressure_db') + uri = os.environ.get('DATABASE_URL', 'postgres://postgres:59fff56880e1bbb42e753d2a82ac21b6@peterstockings.com:15389/bloodpressure_db') if uri and uri.startswith("postgres://"): uri = uri.replace("postgres://", "postgresql://", 1)