handbook_engine / app /main.py
internationalscholarsprogram's picture
fix: ISP handbook styling overhaul - margins, typography, emphasis, benefits, CSS cascade
ec94fc1
"""FastAPI application entry point."""
from __future__ import annotations
import logging
from pathlib import Path
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from app.api.routes import router
from app.core.config import get_settings
from app.core.logging import setup_logging
settings = get_settings()
setup_logging(settings.debug)
logger = logging.getLogger(__name__)
app = FastAPI(
title=settings.app_name,
version=settings.app_version,
docs_url="/docs",
redoc_url="/redoc",
openapi_url="/openapi.json",
)
# CORS — mirrors PHP cors.php allowed origins
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
expose_headers=["Content-Disposition", "Content-Length", "Content-Type"],
)
# Serve static assets (CSS, images) for Playwright to load via file://
# Also accessible at /static/ for debugging
_static_dir = Path(__file__).resolve().parent / "static"
if _static_dir.is_dir():
app.mount("/static", StaticFiles(directory=str(_static_dir)), name="static")
app.include_router(router)
@app.on_event("startup")
async def startup_event():
logger.info(
"%s v%s starting on port %d (debug=%s, renderer=playwright)",
settings.app_name,
settings.app_version,
settings.port,
settings.debug,
)
@app.on_event("shutdown")
async def shutdown_event():
"""Gracefully close the Playwright browser on shutdown."""
from app.services.pdf_renderer import shutdown_browser
await shutdown_browser()
logger.info("Application shutdown complete")