From 90ac8c6b7e8645bc84ee9067fc0a4a4308a73f52 Mon Sep 17 00:00:00 2001 From: itqop Date: Wed, 15 Oct 2025 22:50:21 +0300 Subject: [PATCH] fix: edit config.py --- src/hubgw/api/deps.py | 9 +++++---- src/hubgw/context.py | 11 ++++++----- src/hubgw/core/__init__.py | 5 +++++ src/hubgw/core/config.py | 1 - src/hubgw/core/logging.py | 10 +++++----- src/hubgw/main.py | 25 ++++++++++++------------- 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/hubgw/api/deps.py b/src/hubgw/api/deps.py index d79a52f..7fed23e 100644 --- a/src/hubgw/api/deps.py +++ b/src/hubgw/api/deps.py @@ -1,11 +1,11 @@ """Dependency providers for FastAPI.""" +from collections.abc import AsyncGenerator from fastapi import Depends, HTTPException, Header from sqlalchemy.ext.asyncio import AsyncSession from typing import Annotated from hubgw.context import AppContext -from hubgw.core.config import AppSettings from hubgw.services.homes_service import HomesService from hubgw.services.kits_service import KitsService from hubgw.services.cooldowns_service import CooldownsService @@ -20,7 +20,9 @@ async def get_context() -> AppContext: return AppContext() -async def get_session(context: Annotated[AppContext, Depends(get_context)]) -> AsyncSession: +async def get_session( + context: Annotated[AppContext, Depends(get_context)] +) -> AsyncGenerator[AsyncSession, None]: """Get database session.""" async with context.session_factory() as session: yield session @@ -31,12 +33,11 @@ async def verify_api_key( context: Annotated[AppContext, Depends(get_context)] ) -> str: """Verify API key.""" - if x_api_key != context.settings.API_KEY: + if x_api_key != context.settings.security.api_key: raise HTTPException(status_code=401, detail="Invalid API key") return x_api_key -# Service dependencies def get_homes_service(session: Annotated[AsyncSession, Depends(get_session)]) -> HomesService: """Get homes service.""" return HomesService(session) diff --git a/src/hubgw/context.py b/src/hubgw/context.py index 4b6c430..b535a0a 100644 --- a/src/hubgw/context.py +++ b/src/hubgw/context.py @@ -1,7 +1,7 @@ """Application context singleton.""" from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine -from hubgw.core.config import AppSettings +from hubgw.core.config import APP_CONFIG class AppContext: @@ -16,7 +16,7 @@ class AppContext: def __init__(self): if not hasattr(self, 'initialized'): - self.settings = AppSettings() + self.settings = APP_CONFIG self.engine: AsyncEngine = None self.session_factory: async_sessionmaker = None self.initialized = True @@ -24,9 +24,10 @@ class AppContext: 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.settings.database.dsn, + pool_size=self.settings.database.pool_size, + max_overflow=self.settings.database.max_overflow, + echo=self.settings.database.echo ) self.session_factory = async_sessionmaker( self.engine, diff --git a/src/hubgw/core/__init__.py b/src/hubgw/core/__init__.py index fb31757..fc653bd 100644 --- a/src/hubgw/core/__init__.py +++ b/src/hubgw/core/__init__.py @@ -1 +1,6 @@ """Core module for hubgw.""" +from .config import APP_CONFIG + +__all__ = [ + "APP_CONFIG", +] \ No newline at end of file diff --git a/src/hubgw/core/config.py b/src/hubgw/core/config.py index a82feff..9294bb3 100644 --- a/src/hubgw/core/config.py +++ b/src/hubgw/core/config.py @@ -120,5 +120,4 @@ class Secrets(BaseSettings): ) -# Global configuration instance APP_CONFIG = Secrets() diff --git a/src/hubgw/core/logging.py b/src/hubgw/core/logging.py index e2e8af0..52effa0 100644 --- a/src/hubgw/core/logging.py +++ b/src/hubgw/core/logging.py @@ -2,12 +2,12 @@ import sys from loguru import logger -from hubgw.core.config import AppSettings +from hubgw.core.config import APP_CONFIG def setup_logging(): """Setup loguru logging configuration.""" - settings = AppSettings() + settings = APP_CONFIG.app # Remove default handler logger.remove() @@ -15,16 +15,16 @@ def setup_logging(): # Add console handler logger.add( sys.stdout, - level=settings.APP_LOG_LEVEL, + level=settings.log_level, format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}", colorize=True ) # Add file handler for production - if settings.APP_ENV == "prod": + if settings.env == "prod": logger.add( "logs/hubgw.log", - level=settings.APP_LOG_LEVEL, + level=settings.log_level, format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}", rotation="1 day", retention="30 days", diff --git a/src/hubgw/main.py b/src/hubgw/main.py index ab26d03..a498e29 100644 --- a/src/hubgw/main.py +++ b/src/hubgw/main.py @@ -1,32 +1,31 @@ """FastAPI application factory.""" - +from contextlib import asynccontextmanager from fastapi import FastAPI from hubgw.core.logging import setup_logging from hubgw.context import AppContext +@asynccontextmanager +async def lifespan(app: FastAPI): + """Application lifespan context.""" + ctx = AppContext() + await ctx.startup() + yield + await ctx.shutdown() + + def create_app() -> FastAPI: """Create and configure FastAPI application.""" app = FastAPI( title="HubGW", description="FastAPI Gateway for HubMC", - version="0.1.0" + version="0.1.0", + lifespan=lifespan ) # Setup logging setup_logging() - # Initialize context - ctx = AppContext() - - @app.on_event("startup") - async def startup(): - await ctx.startup() - - @app.on_event("shutdown") - async def shutdown(): - await ctx.shutdown() - # Include routers from hubgw.api.v1.router import api_router app.include_router(api_router, prefix="/api/v1")