""" passenger_wsgi.py WSGI entry point for the DataBaser Flask application. Located at: /home7/smarthoo/apps/databaser/app/passenger_wsgi.py A symlink from /home7/smarthoo/public_html/databaser/passenger_wsgi.py will point to this file so Passenger can load it. """ import sys import os import logging from pathlib import Path from logging.handlers import RotatingFileHandler # ------------------------------------------------------------------------------ # 1) Configure logging with fallbacks # ------------------------------------------------------------------------------ CURRENT_DIR = Path(__file__).resolve().parent LOG_LOCATIONS = [ CURRENT_DIR / "logs", CURRENT_DIR / "tmp", Path("/tmp") ] def setup_logging(): logger = logging.getLogger("passenger_wsgi") logger.setLevel(logging.INFO) # Add stderr handler as fallback stderr_handler = logging.StreamHandler(sys.stderr) stderr_handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(name)s - %(message)s")) logger.addHandler(stderr_handler) # Try each log location until one works for log_dir in LOG_LOCATIONS: try: if not log_dir.exists(): log_dir.mkdir(mode=0o777, parents=True, exist_ok=True) log_file = log_dir / "passenger.log" handler = RotatingFileHandler( log_file, maxBytes=1_048_576, # 1 MB backupCount=3, mode='a' # Append mode ) formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(name)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # Test write handler.stream.write("") handler.stream.flush() logger.info(f"Successfully set up logging to {log_file}") return logger except (OSError, IOError) as e: logger.warning(f"Could not set up logging in {log_dir}: {e}") continue logger.warning("Could not set up file logging, falling back to stderr only") return logger logger = setup_logging() logger.info("Starting passenger_wsgi.py...") # ------------------------------------------------------------------------------ # 2) Ensure this directory is on sys.path # ------------------------------------------------------------------------------ if str(CURRENT_DIR) not in sys.path: sys.path.insert(0, str(CURRENT_DIR)) logger.info(f"Added {CURRENT_DIR} to sys.path") # ------------------------------------------------------------------------------ # 3) Import the Flask `app` from app.py as `application` # ------------------------------------------------------------------------------ try: from app import app as application logger.info("Flask app imported successfully from app.py!") logger.info(f"Registered routes: {[str(r) for r in application.url_map.iter_rules()]}") except Exception as ex: logger.exception("Error loading Flask application", exc_info=ex) raise # ------------------------------------------------------------------------------ # 4) The WSGI callable: Passenger will call `application(environ, start_response)` # automatically. If you want to log each request, wrap it or add a debug log. # ------------------------------------------------------------------------------