"""Application context singleton.""" from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine from hubgw.core.config import AppSettings class AppContext: """Application context singleton.""" _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance def __init__(self): if not hasattr(self, 'initialized'): self.settings = AppSettings() self.engine: AsyncEngine = None self.session_factory: async_sessionmaker = None self.initialized = True async def startup(self): """Initialize database engine and session factory.""" self.engine = create_async_engine( self.settings.DB_DSN, pool_size=self.settings.DB_POOL_SIZE, max_overflow=self.settings.DB_MAX_OVERFLOW ) self.session_factory = async_sessionmaker( self.engine, expire_on_commit=False ) async def shutdown(self): """Close database engine.""" if self.engine: await self.engine.dispose()