35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
"""Health check router.
|
|
|
|
A lightweight ``GET /api/health`` endpoint that verifies the application
|
|
is running and can serve requests. Also reports the cached fail2ban liveness
|
|
state so monitoring tools and Docker health checks can observe daemon status
|
|
without probing the socket directly.
|
|
"""
|
|
|
|
from fastapi import APIRouter
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.dependencies import ServerStatusDep
|
|
|
|
router: APIRouter = APIRouter(prefix="/api", tags=["Health"])
|
|
|
|
|
|
@router.get("/health", summary="Application health check")
|
|
async def health_check(server_status: ServerStatusDep) -> JSONResponse:
|
|
"""Return 200 with application and fail2ban status.
|
|
|
|
HTTP 200 is always returned so Docker health checks do not restart the
|
|
backend container when fail2ban is temporarily offline. The
|
|
``fail2ban`` field in the body indicates the daemon's current state.
|
|
|
|
Args:
|
|
server_status: Injected cached server status snapshot.
|
|
|
|
Returns:
|
|
A JSON object with ``{"status": "ok", "fail2ban": "online"|"offline"}``.
|
|
"""
|
|
return JSONResponse(content={
|
|
"status": "ok",
|
|
"fail2ban": "online" if server_status.online else "offline",
|
|
})
|