diff --git a/.env.development b/.env.development index 41baa8b..0968536 100644 --- a/.env.development +++ b/.env.development @@ -11,4 +11,5 @@ DB_NAME=mold_cost_development # --- Application Configuration --- SECRET_KEY='a_different_secret_key_for_development' -FLASK_DEBUG=1 \ No newline at end of file +FLASK_DEBUG=1 +HOST_ENV=local # Set AWS for AWS deployments \ No newline at end of file diff --git a/src/app/__init__.py b/src/app/__init__.py index e169cdc..c090482 100644 --- a/src/app/__init__.py +++ b/src/app/__init__.py @@ -35,15 +35,17 @@ def create_app(config_class=None): if config_class is None: config_class = get_config() app.config.from_object(config_class) - + + # Ensure SECRET_KEY is set from environment if not present + if not hasattr(config_class, 'SECRET_KEY') or not config_class.SECRET_KEY: + env_secret = os.environ.get('SECRET_KEY') + if not env_secret: + raise ValueError("SECRET_KEY environment variable is not set") + config_class.SECRET_KEY = env_secret + app.config['SECRET_KEY'] = config_class.SECRET_KEY.encode('utf-8') # Initialize configuration config_class.init_app(app) - # Ensure secret key is properly set - if not app.config.get('SECRET_KEY'): - raise ValueError("SECRET_KEY must be set in the configuration") - app.config['SECRET_KEY'] = app.config['SECRET_KEY'].encode('utf-8') - # Ensure instance folder exists try: os.makedirs(app.instance_path) @@ -51,7 +53,8 @@ def create_app(config_class=None): pass # Configure logging - configure_logging(app) + if os.environ.get('HOST_ENV') != 'AWS': + configure_logging(app) # Initialize extensions with retry logic max_retries = 3 @@ -117,8 +120,8 @@ def create_app(config_class=None): handler.flush() # Debug: write all registered endpoints to a file - with open('logs/endpoints.log', 'w') as f: - for rule in app.url_map.iter_rules(): - f.write(f"{rule.endpoint} {rule.methods} {rule.rule}\n") + # with open('logs/endpoints.log', 'w') as f: + # for rule in app.url_map.iter_rules(): + # f.write(f"{rule.endpoint} {rule.methods} {rule.rule}\n") return app \ No newline at end of file diff --git a/src/app/config.py b/src/app/config.py index 0168560..fae64bf 100644 --- a/src/app/config.py +++ b/src/app/config.py @@ -164,14 +164,15 @@ class Config: app.config['SQLALCHEMY_DATABASE_URI'] = cls.SQLALCHEMY_DATABASE_URI # Configure logging - if not os.path.exists('logs'): - os.mkdir('logs') - file_handler = RotatingFileHandler('logs/app.log', - maxBytes=10240, - backupCount=10) - file_handler.setFormatter(logging.Formatter(cls.LOG_FORMAT)) - file_handler.setLevel(logging.INFO) - app.logger.addHandler(file_handler) + if os.environ.get('HOST_ENV') != 'AWS': + if not os.path.exists('logs'): + os.mkdir('logs') + file_handler = RotatingFileHandler('logs/app.log', + maxBytes=10240, + backupCount=10) + file_handler.setFormatter(logging.Formatter(cls.LOG_FORMAT)) + file_handler.setLevel(logging.INFO) + app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) app.logger.info('Application startup') diff --git a/src/requirements.txt b/src/requirements.txt index 930b528..4ebec22 100644 Binary files a/src/requirements.txt and b/src/requirements.txt differ diff --git a/src/run.py b/src/run.py index 42fc87d..2ee09fc 100644 --- a/src/run.py +++ b/src/run.py @@ -5,6 +5,8 @@ import os import logging from logging.handlers import RotatingFileHandler from flask import render_template +from flask_migrate import upgrade +import awsgi app = create_app() @@ -39,4 +41,14 @@ if not app.debug: app.logger.info('Mold Cost Calculator startup') if __name__ == '__main__': - app.run(host='0.0.0.0', port=5004) \ No newline at end of file + app.run(host='0.0.0.0', port=5004) + +def lambda_handler(event, context): + return awsgi.response(app, event, context) + +def lambda_handler_upgrade_db(event, context): + with app.app_context(): + upgrade() + return {"statusCode": 200, "body": "Database upgraded successfully"} + +handler = Mangum(app) \ No newline at end of file