internationalscholarsprogram/handbook-engine
0
1"""FastAPI application entry point."""2 3from __future__ import annotations4 5import logging6from pathlib import Path7 8from fastapi import FastAPI9from fastapi.middleware.cors import CORSMiddleware10from fastapi.staticfiles import StaticFiles11 12from app.api.routes import router13from app.core.config import get_settings14from app.core.logging import setup_logging15 16settings = get_settings()17setup_logging(settings.debug)18 19logger = logging.getLogger(__name__)20 21app = FastAPI(22 title=settings.app_name,23 version=settings.app_version,24 docs_url="/docs",25 redoc_url="/redoc",26 openapi_url="/openapi.json",27)28 29# CORS — allow any origin so production, staging, and local all work30app.add_middleware(31 CORSMiddleware,32 allow_origins=["*"],33 allow_credentials=False,34 allow_methods=["GET", "POST", "OPTIONS"],35 allow_headers=["*"],36 expose_headers=["Content-Disposition", "Content-Length", "Content-Type"],37)38 39# Serve static assets (CSS, images) for Playwright to load via file://40# Also accessible at /static/ for debugging41_static_dir = Path(__file__).resolve().parent / "static"42if _static_dir.is_dir():43 app.mount("/static", StaticFiles(directory=str(_static_dir)), name="static")44 45app.include_router(router)46 47 48@app.on_event("startup")49async def startup_event():50 logger.info(51 "%s v%s starting on port %d (debug=%s, renderer=playwright)",52 settings.app_name,53 settings.app_version,54 settings.port,55 settings.debug,56 )57 58 59@app.on_event("shutdown")60async def shutdown_event():61 """Gracefully close the Playwright browser on shutdown."""62 from app.services.pdf_renderer import shutdown_browser63 await shutdown_browser()64 logger.info("Application shutdown complete")65 