23f3003322's picture
new changes to handle dificulty level 1
dc1c6a7
"""
Main FastAPI Application Entry Point
Initializes the FastAPI app with all configurations, middleware, and routes
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from app.core.config import settings
from app.core.logging import setup_logging, get_logger
from app.core.exceptions import register_exception_handlers
from app.middleware.logging import LoggingMiddleware
from app.api.routes import task, health
from app.core.logging import setup_logging, get_logger
from app.modules import register_all_modules
from app.orchestrator.orchestrator_engine import OrchestratorEngine
# Initialize logger
logger = get_logger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan with log flushing"""
# Startup
logger.info("=" * 80)
logger.info("πŸš€ Starting LLM Analysis Quiz API")
logger.info(f"Environment: {settings.ENVIRONMENT}")
logger.info("=" * 80)
import os
# if os.getenv('ENVIRONMENT') == 'production':
# from app.modules.scrapers.browser_pool import get_pooled_browser
# from app.modules.scrapers.browser_config import PRODUCTION_CONFIG
# logger.info("Pre-warming browser pool...")
# await get_pooled_browser(PRODUCTION_CONFIG)
# logger.info("βœ“ Browser pool ready")
yield
# Shutdown
logger.info("=" * 80)
logger.info("πŸ›‘ Shutting down - flushing logs")
logger.info("=" * 80)
# Cleanup browser pool
from app.modules.scrapers.browser_pool import BrowserPool
await BrowserPool.cleanup()
def create_application() -> FastAPI:
"""
Application factory pattern for creating FastAPI instance
"""
app = FastAPI(
title=settings.APP_NAME,
description=settings.APP_DESCRIPTION,
version=settings.APP_VERSION,
lifespan=lifespan,
# docs_url="/docs" if settings.ENVIRONMENT == "development" else None,
# redoc_url="/redoc" if settings.ENVIRONMENT == "development" else None,
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add custom middleware
app.add_middleware(LoggingMiddleware)
# Register exception handlers
register_exception_handlers(app)
registry = register_all_modules()
# orchestrator = OrchestratorEngine(registry)
# Include routers
app.include_router(health.router, tags=["Health"])
app.include_router(task.router, tags=["Tasks"])
return app
# Initialize logging
setup_logging()
# Create app instance
app = create_application()
if __name__ == "__main__":
import uvicorn
logger.info(f"Starting server on {settings.HOST}:{settings.PORT}")
uvicorn.run(
"app.main:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.ENVIRONMENT == "development",
log_level="info"
)