chore: migrate @app.on_event to FastAPI lifespan handler
Some checks failed
CI / test (push) Failing after 21s
Some checks failed
CI / test (push) Failing after 21s
Replaces the deprecated @app.on_event('startup') decorator with
the asynccontextmanager lifespan pattern. Startup logic (.env load
+ db migration) moves into lifespan(); no shutdown logic needed.
Closes #70
This commit is contained in:
parent
3458122537
commit
c0649c7328
1 changed files with 14 additions and 9 deletions
23
dev-api.py
23
dev-api.py
|
|
@ -24,6 +24,8 @@ from urllib.parse import urlparse
|
||||||
import requests
|
import requests
|
||||||
import yaml
|
import yaml
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from fastapi import FastAPI, HTTPException, Request, Response, UploadFile
|
from fastapi import FastAPI, HTTPException, Request, Response, UploadFile
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
@ -46,7 +48,18 @@ _DIRECTUS_SECRET = os.environ.get("DIRECTUS_JWT_SECRET", "")
|
||||||
# Per-request DB path — set by cloud_session_middleware; falls back to DB_PATH
|
# Per-request DB path — set by cloud_session_middleware; falls back to DB_PATH
|
||||||
_request_db: ContextVar[str | None] = ContextVar("_request_db", default=None)
|
_request_db: ContextVar[str | None] = ContextVar("_request_db", default=None)
|
||||||
|
|
||||||
app = FastAPI(title="Peregrine Dev API")
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
"""Load .env then run pending SQLite migrations on startup."""
|
||||||
|
# Load .env before any runtime env reads — safe because lifespan doesn't run
|
||||||
|
# when dev_api is imported by tests (only when uvicorn actually starts).
|
||||||
|
_load_env(PEREGRINE_ROOT / ".env")
|
||||||
|
from scripts.db_migrate import migrate_db
|
||||||
|
migrate_db(Path(DB_PATH))
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(title="Peregrine Dev API", lifespan=lifespan)
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
|
|
@ -139,14 +152,6 @@ def _strip_html(text: str | None) -> str | None:
|
||||||
return cleaned.strip() or None
|
return cleaned.strip() or None
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
def _startup():
|
|
||||||
"""Load .env then run pending SQLite migrations."""
|
|
||||||
# Load .env before any runtime env reads — safe because startup doesn't run
|
|
||||||
# when dev_api is imported by tests (only when uvicorn actually starts).
|
|
||||||
_load_env(PEREGRINE_ROOT / ".env")
|
|
||||||
from scripts.db_migrate import migrate_db
|
|
||||||
migrate_db(Path(DB_PATH))
|
|
||||||
|
|
||||||
|
|
||||||
# ── Link extraction helpers ───────────────────────────────────────────────
|
# ── Link extraction helpers ───────────────────────────────────────────────
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue