from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager from app.api.v1.api import api_router from app.core.config import settings from app.db.session import SessionLocal from app.db.init_db import init_db @asynccontextmanager async def lifespan(app: FastAPI): # Startup db = SessionLocal() try: init_db(db) finally: db.close() yield # Shutdown app = FastAPI( title=settings.PROJECT_NAME, version=settings.VERSION, description="School Portal API - A comprehensive school management system", openapi_url="/openapi.json", docs_url="/docs", redoc_url="/redoc", lifespan=lifespan ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(api_router, prefix="/api/v1") @app.get("/") async def root(): return { "title": settings.PROJECT_NAME, "version": settings.VERSION, "description": "School Portal API - A comprehensive school management system", "documentation": "/docs", "health": "/health" } @app.get("/health") async def health_check(): return {"status": "healthy", "service": "School Portal API"}