
- Update import from pydantic.BaseSettings to pydantic_settings.BaseSettings
- Add pydantic-settings==2.1.0 to requirements.txt for Pydantic v2 compatibility
- Implement application lifespan management for database initialization
- Add init_db.py for automatic database table creation and superuser setup
- Create test_imports.py for verifying import integrity
- Ensure proper startup sequence and error handling
This fixes the health check failure caused by Pydantic v2 migration changes.
🤖 Generated with BackendIM
Co-Authored-By: BackendIM <noreply@anthropic.com>
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
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"} |