Core App changes to make it AWS compatible
This commit is contained in:
@@ -12,3 +12,4 @@ DB_NAME=mold_cost_development
|
|||||||
# --- Application Configuration ---
|
# --- Application Configuration ---
|
||||||
SECRET_KEY='a_different_secret_key_for_development'
|
SECRET_KEY='a_different_secret_key_for_development'
|
||||||
FLASK_DEBUG=1
|
FLASK_DEBUG=1
|
||||||
|
HOST_ENV=local # Set AWS for AWS deployments
|
||||||
+11
-8
@@ -36,14 +36,16 @@ def create_app(config_class=None):
|
|||||||
config_class = get_config()
|
config_class = get_config()
|
||||||
app.config.from_object(config_class)
|
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
|
# Initialize configuration
|
||||||
config_class.init_app(app)
|
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
|
# Ensure instance folder exists
|
||||||
try:
|
try:
|
||||||
os.makedirs(app.instance_path)
|
os.makedirs(app.instance_path)
|
||||||
@@ -51,6 +53,7 @@ def create_app(config_class=None):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
|
if os.environ.get('HOST_ENV') != 'AWS':
|
||||||
configure_logging(app)
|
configure_logging(app)
|
||||||
|
|
||||||
# Initialize extensions with retry logic
|
# Initialize extensions with retry logic
|
||||||
@@ -117,8 +120,8 @@ def create_app(config_class=None):
|
|||||||
handler.flush()
|
handler.flush()
|
||||||
|
|
||||||
# Debug: write all registered endpoints to a file
|
# Debug: write all registered endpoints to a file
|
||||||
with open('logs/endpoints.log', 'w') as f:
|
# with open('logs/endpoints.log', 'w') as f:
|
||||||
for rule in app.url_map.iter_rules():
|
# for rule in app.url_map.iter_rules():
|
||||||
f.write(f"{rule.endpoint} {rule.methods} {rule.rule}\n")
|
# f.write(f"{rule.endpoint} {rule.methods} {rule.rule}\n")
|
||||||
|
|
||||||
return app
|
return app
|
||||||
@@ -164,6 +164,7 @@ class Config:
|
|||||||
app.config['SQLALCHEMY_DATABASE_URI'] = cls.SQLALCHEMY_DATABASE_URI
|
app.config['SQLALCHEMY_DATABASE_URI'] = cls.SQLALCHEMY_DATABASE_URI
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
|
if os.environ.get('HOST_ENV') != 'AWS':
|
||||||
if not os.path.exists('logs'):
|
if not os.path.exists('logs'):
|
||||||
os.mkdir('logs')
|
os.mkdir('logs')
|
||||||
file_handler = RotatingFileHandler('logs/app.log',
|
file_handler = RotatingFileHandler('logs/app.log',
|
||||||
|
|||||||
Binary file not shown.
+12
@@ -5,6 +5,8 @@ import os
|
|||||||
import logging
|
import logging
|
||||||
from logging.handlers import RotatingFileHandler
|
from logging.handlers import RotatingFileHandler
|
||||||
from flask import render_template
|
from flask import render_template
|
||||||
|
from flask_migrate import upgrade
|
||||||
|
import awsgi
|
||||||
|
|
||||||
app = create_app()
|
app = create_app()
|
||||||
|
|
||||||
@@ -40,3 +42,13 @@ if not app.debug:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=5004)
|
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)
|
||||||
Reference in New Issue
Block a user