""" Dynamic admin module loader that chooses between full and simple admin based on service availability """ import os import logging from dotenv import load_dotenv # Load environment variables load_dotenv() logger = logging.getLogger(__name__) def load_admin_module(): """ Dynamically load admin module based on service availability Returns (admin_bp, init_admin) tuple """ # Check if we should force simple mode if os.environ.get('ADMIN_SIMPLE_MODE', '').lower() in ('1', 'true', 'yes'): logger.info("Simple admin mode forced by environment variable") from admin_simple import admin_bp, init_admin return admin_bp, init_admin # Try to import full admin module try: # Quick check for Redis import redis r = redis.Redis.from_url(os.environ.get('REDIS_URL', 'redis://localhost:6379/0')) r.ping() # Quick check for PostgreSQL from sqlalchemy import create_engine, text db_url = os.environ.get('DATABASE_URL', 'postgresql://localhost/talk2me') engine = create_engine(db_url, pool_pre_ping=True) with engine.connect() as conn: conn.execute(text("SELECT 1")) # If we get here, both services are available from admin import admin_bp, init_admin logger.info("Using full admin module with Redis and PostgreSQL support") return admin_bp, init_admin except Exception as e: logger.warning(f"Cannot use full admin module: {e}") logger.info("Falling back to simple admin module") from admin_simple import admin_bp, init_admin return admin_bp, init_admin