From ca614328ac9fca4b34203881eb55053a9ae7291e Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Fri, 6 Feb 2026 11:57:29 +0530 Subject: [PATCH] feat: implement async lifespan for FastAPI and remove startup event --- backend/app/main.py | 16 ++++++++++------ backend/pyproject.toml | 3 +++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/app/main.py b/backend/app/main.py index a1e36b8e..025c19dc 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1,5 +1,7 @@ from __future__ import annotations +from contextlib import asynccontextmanager + from fastapi import APIRouter, FastAPI from fastapi.middleware.cors import CORSMiddleware @@ -22,7 +24,14 @@ from app.db.session import init_db configure_logging() -app = FastAPI(title="Mission Control API", version="0.1.0") + +@asynccontextmanager +async def lifespan(_: FastAPI): + init_db() + yield + + +app = FastAPI(title="Mission Control API", version="0.1.0", lifespan=lifespan) origins = [o.strip() for o in settings.cors_origins.split(",") if o.strip()] if origins: @@ -35,11 +44,6 @@ if origins: ) -@app.on_event("startup") -def on_startup() -> None: - init_db() - - @app.get("/health") def health() -> dict[str, bool]: return {"ok": True} diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 2fb0d368..6e8ff779 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -47,3 +47,6 @@ warn_redundant_casts = true warn_unused_configs = true check_untyped_defs = true plugins = ["pydantic.mypy"] + +[tool.pytest.ini_options] +asyncio_default_fixture_loop_scope = "function"