04/28 Fixed bugs
This commit is contained in:
@@ -49,6 +49,13 @@ def create_app() -> Flask:
|
||||
cfg = get_config()
|
||||
app.config.from_object(cfg)
|
||||
|
||||
# Guard against deployment with the insecure default SECRET_KEY
|
||||
import sys
|
||||
if not app.debug and app.config.get('SECRET_KEY') == 'change-me-in-production':
|
||||
print("FATAL: SECRET_KEY is set to the insecure default value. "
|
||||
"Set SECRET_KEY in your .env file before deploying to production.")
|
||||
sys.exit(1)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Database initialization
|
||||
# ------------------------------------------------------------------
|
||||
@@ -100,12 +107,8 @@ def create_app() -> Flask:
|
||||
|
||||
@app.context_processor
|
||||
def inject_company_name():
|
||||
"""Make COMPANY_NAME, THEME_NAME, and CURRENT_YEAR available to all templates"""
|
||||
return {
|
||||
'COMPANY_NAME': os.environ.get('COMPANY_NAME', 'QR Code Management System'),
|
||||
'THEME_NAME': os.environ.get('THEME_NAME', ''),
|
||||
'CURRENT_YEAR': datetime.now().year,
|
||||
}
|
||||
"""Make COMPANY_NAME available to all templates"""
|
||||
return {'COMPANY_NAME': os.environ.get('COMPANY_NAME', 'QR Code Management System')}
|
||||
|
||||
@app.context_processor
|
||||
def inject_logging_status():
|
||||
@@ -247,10 +250,20 @@ def create_app() -> Flask:
|
||||
@app.errorhandler(500)
|
||||
def internal_error(error):
|
||||
"""Handle internal server errors with user-friendly page"""
|
||||
if app.debug:
|
||||
return None
|
||||
return render_template('errors/500.html'), 500
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Startup initialization (runs under gunicorn and flask run alike)
|
||||
# ------------------------------------------------------------------
|
||||
with app.app_context():
|
||||
try:
|
||||
create_tables()
|
||||
update_existing_qr_codes()
|
||||
except Exception as e:
|
||||
from extensions import logger_handler as _startup_lh
|
||||
_startup_lh.logger.error(f"Startup initialization failed: {e}", exc_info=True)
|
||||
raise
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@@ -298,10 +311,8 @@ def update_existing_qr_codes():
|
||||
"""Update existing QR codes with missing URLs or images at startup.
|
||||
|
||||
Regenerates qr_url slugs without needing a request context.
|
||||
For qr_code_image, uses QR_BASE_URL (from .env) as the authoritative base
|
||||
URL so that generated links always match the public-facing domain.
|
||||
Falls back to FLASK_HOST/FLASK_PORT construction only when QR_BASE_URL is
|
||||
not configured (development environments without a reverse proxy).
|
||||
For qr_code_image, constructs the base URL from FLASK_HOST/FLASK_PORT
|
||||
config so this can run safely outside any HTTP request.
|
||||
"""
|
||||
from extensions import db as _db, logger_handler as lh
|
||||
from utils.helpers import generate_qr_code, get_qr_styling, generate_qr_url
|
||||
@@ -312,19 +323,14 @@ def update_existing_qr_codes():
|
||||
if not qr_codes:
|
||||
return
|
||||
|
||||
# Prefer the explicit QR_BASE_URL env var (required behind a reverse proxy).
|
||||
# Fall back to FLASK_HOST/PORT for local/dev environments.
|
||||
qr_base_url = os.environ.get('QR_BASE_URL', '').rstrip('/')
|
||||
if qr_base_url:
|
||||
base_url = qr_base_url + '/'
|
||||
else:
|
||||
host = os.environ.get('FLASK_HOST', '0.0.0.0')
|
||||
# 0.0.0.0 is a bind address, not a reachable hostname — default to localhost
|
||||
if host in ('0.0.0.0', ''):
|
||||
host = 'localhost'
|
||||
port = os.environ.get('FLASK_PORT', '5000')
|
||||
scheme = 'https' if _Cfg.SESSION_COOKIE_SECURE else 'http'
|
||||
base_url = f"{scheme}://{host}:{port}/"
|
||||
# Build a base URL that does not require an active request context.
|
||||
host = os.environ.get('FLASK_HOST', '0.0.0.0')
|
||||
# 0.0.0.0 is a bind address, not a reachable hostname — default to localhost
|
||||
if host in ('0.0.0.0', ''):
|
||||
host = 'localhost'
|
||||
port = os.environ.get('FLASK_PORT', '5000')
|
||||
scheme = 'https' if _Cfg.SESSION_COOKIE_SECURE else 'http'
|
||||
base_url = f"{scheme}://{host}:{port}/"
|
||||
|
||||
updated_count = 0
|
||||
for qr_code in qr_codes:
|
||||
@@ -363,9 +369,6 @@ app = create_app()
|
||||
if __name__ == '__main__':
|
||||
with app.app_context():
|
||||
try:
|
||||
create_tables()
|
||||
update_existing_qr_codes()
|
||||
|
||||
from extensions import logger_handler
|
||||
logger_handler.logger.info("Initializing performance optimizations")
|
||||
cached_query = initialize_performance_optimizations(app, db, logger_handler)
|
||||
|
||||
Reference in New Issue
Block a user