51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi import Request
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.auth import router as auth_router
|
|
from app.api.children import router as children_router
|
|
from app.api.parents import router as parents_router
|
|
from app.api.health import router as health_router
|
|
from app.core.config import settings
|
|
from app.db.mongo import close_mongo_connection, connect_to_mongo, get_database
|
|
from app.services.records import ensure_record_indexes
|
|
from app.services.auth import ensure_auth_indexes
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
await connect_to_mongo()
|
|
await ensure_auth_indexes(get_database())
|
|
await ensure_record_indexes(get_database())
|
|
yield
|
|
await close_mongo_connection()
|
|
|
|
|
|
app = FastAPI(title="Zero Knowledge Platform API", lifespan=lifespan)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(health_router)
|
|
app.include_router(auth_router)
|
|
app.include_router(parents_router)
|
|
app.include_router(children_router)
|
|
|
|
|
|
@app.exception_handler(Exception)
|
|
async def unhandled_exception_handler(request: Request, exc: Exception) -> JSONResponse:
|
|
return JSONResponse(status_code=500, content={"detail": "Internal server error."})
|
|
|
|
|
|
@app.get("/")
|
|
async def root() -> dict[str, str]:
|
|
return {"message": "Zero Knowledge Platform API"}
|