Core App changes to make it AWS compatible

This commit is contained in:
Anup Raj Gopinathan
2025-06-25 10:15:06 +02:00
parent a1694d1b75
commit b5849c7fbd
5 changed files with 37 additions and 20 deletions
+1
View File
@@ -12,3 +12,4 @@ DB_NAME=mold_cost_development
# --- Application Configuration ---
SECRET_KEY='a_different_secret_key_for_development'
FLASK_DEBUG=1
HOST_ENV=local # Set AWS for AWS deployments
+12 -9
View File
@@ -36,14 +36,16 @@ def create_app(config_class=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
+9 -8
View File
@@ -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')
Binary file not shown.
+12
View File
@@ -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()
@@ -40,3 +42,13 @@ if not app.debug:
if __name__ == '__main__':
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)