From 6fbdcef308366631d5a021b82ec4a9223e3d84e8 Mon Sep 17 00:00:00 2001 From: LucifersCircle Date: Sat, 7 Dec 2024 15:18:12 -0800 Subject: [PATCH] Update app.py set up Gunicorn logging make db initialization happen once instead of on every runner --- app.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 5c22bd9..cdacaae 100644 --- a/app.py +++ b/app.py @@ -2,12 +2,19 @@ import re import sqlite3 import hashlib +import logging from flask import Flask, request, jsonify, render_template_string from cryptography.fernet import Fernet app = Flask(__name__) +# Set up logging for Gunicorn +if __name__ != '__main__': + gunicorn_logger = logging.getLogger('gunicorn.error') + app.logger.handlers = gunicorn_logger.handlers + app.logger.setLevel(gunicorn_logger.level) + # Get encryption key from environment variable encryption_key = os.getenv('ENCRYPTION_KEY') if not encryption_key: @@ -43,9 +50,6 @@ def initialize_db(): except Exception as e: print(f"Error initializing database: {e}", flush=True) -# Initialize database on startup -initialize_db() - # HTML template for the landing page HTML_TEMPLATE = """ @@ -143,8 +147,6 @@ def initialize_db(): """ - - # Landing page route @app.route('/', methods=['GET', 'POST']) def manage_key(): @@ -226,4 +228,5 @@ def manage_key(): if __name__ == '__main__': + initialize_db() # Initialize database on startup app.run(host='0.0.0.0', port=5000)