Add /health and /ready for scanner

This commit is contained in:
2025-11-09 19:45:47 +01:00
parent a95bbcb6eb
commit b4c85f3f28
2 changed files with 26 additions and 1 deletions

View File

@@ -49,6 +49,12 @@ async def get_db():
yield cast(Connection, db)
# because https://github.com/fastapi/fastapi/pull/10353
async def get_db_fapi():
async with get_db() as db:
yield db
async def migrate(migrations_dir="./migrations"):
async with get_db() as db:
_ = await db.execute(

View File

@@ -1,6 +1,9 @@
from typing import Annotated
from fastapi import APIRouter, BackgroundTasks, Security
from asyncpg import Connection
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Security
from scanner.database import get_db_fapi
from ..fsscan import create_scanner
from ..jwt import validate_bearer
@@ -26,3 +29,19 @@ async def trigger_scan(
await scanner.scan()
tasks.add_task(run)
@router.get("/health")
def get_health():
return {"status": "healthy"}
@router.get("/ready")
async def get_ready(db: Annotated[Connection, Depends(get_db_fapi)]):
try:
_ = await db.execute("select 1")
return {"status": "healthy", "database": "healthy"}
except Exception as e:
raise HTTPException(
status_code=500, detail={"status": "unhealthy", "database": str(e)}
)