Spaces:
Sleeping
Sleeping
File size: 3,091 Bytes
d1bf1d0 b13af0a 927f808 d1bf1d0 b13af0a d1bf1d0 2a327ca dc1c6a7 2a327ca dc1c6a7 b13af0a d1bf1d0 b13af0a d1bf1d0 2a327ca b13af0a d1bf1d0 dc1c6a7 d1bf1d0 927f808 dc1c6a7 d1bf1d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
"""
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"
)
|