feat(health): add /healthz endpoint (#26)

This commit is contained in:
Jeff Smith 2026-04-19 11:53:09 -06:00
parent ea9e76d090
commit 7e59d3432d
3 changed files with 39 additions and 0 deletions

View file

@ -6,6 +6,7 @@ from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from quartermaster.routes import router from quartermaster.routes import router
from quartermaster.routes_health import router as health_router
from quartermaster.routes_month import router as month_router from quartermaster.routes_month import router as month_router
STATIC_DIR = Path(__file__).parent / "static" STATIC_DIR = Path(__file__).parent / "static"
@ -14,6 +15,7 @@ STATIC_DIR = Path(__file__).parent / "static"
def create_app() -> FastAPI: def create_app() -> FastAPI:
app = FastAPI(title="Quartermaster", version="0.1.0") app = FastAPI(title="Quartermaster", version="0.1.0")
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static") app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
app.include_router(health_router)
app.include_router(router) app.include_router(router)
app.include_router(month_router) app.include_router(month_router)
return app return app

View file

@ -0,0 +1,30 @@
from __future__ import annotations
import logging
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
from sqlalchemy import text
from sqlalchemy.orm import Session
from quartermaster.db import get_session
logger = logging.getLogger("quartermaster.health")
router = APIRouter()
@router.get("/healthz")
def healthz(db: Session = Depends(get_session)) -> JSONResponse:
try:
db.execute(text("SELECT 1"))
except Exception as exc:
logger.warning(
"healthz check failed",
extra={"event": "healthz_failed", "error_class": type(exc).__name__},
)
return JSONResponse(
status_code=503,
content={"status": "error", "detail": type(exc).__name__},
)
return JSONResponse(status_code=200, content={"status": "ok"})

7
tests/test_health.py Normal file
View file

@ -0,0 +1,7 @@
from __future__ import annotations
def test_healthz_returns_ok(client):
response = client.get("/healthz")
assert response.status_code == 200
assert response.json() == {"status": "ok"}