Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi.responses import JSONResponse

from nene2.config import AppSettings
from nene2.http import HealthStatus
from nene2.middleware import ErrorHandlerMiddleware
from nene2.validation.exceptions import ValidationException

Expand Down Expand Up @@ -73,9 +74,11 @@ def create_app(settings: AppSettings | None = None) -> FastAPI:
)
)

@app.get("/health")
@app.get("/health", tags=["system"], summary="Health check")
async def health() -> JSONResponse:
return JSONResponse({"status": "ok"})
status = HealthStatus(status="ok")
code = 200 if status.is_healthy else 503
return JSONResponse({"status": status.status, "checks": status.checks}, status_code=code)

return app

Expand Down
5 changes: 4 additions & 1 deletion src/nene2/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""HTTP helpers — JSON responses, pagination, problem details."""
"""HTTP helpers — JSON responses, pagination, problem details, health."""

from .health import HealthCheckProtocol, HealthStatus
from .pagination import PaginationQuery, PaginationQueryParser, PaginationResponse
from .problem_details import problem_details_response

__all__ = [
"HealthCheckProtocol",
"HealthStatus",
"PaginationQuery",
"PaginationQueryParser",
"PaginationResponse",
Expand Down
20 changes: 20 additions & 0 deletions src/nene2/http/health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""HealthCheckProtocol and HealthStatus — framework health check contract."""

from dataclasses import dataclass, field
from typing import Protocol


@dataclass(frozen=True, slots=True)
class HealthStatus:
status: str
checks: dict[str, str] = field(default_factory=dict)

@property
def is_healthy(self) -> bool:
return self.status == "ok"


class HealthCheckProtocol(Protocol):
"""Contract for application health checks."""

def check(self) -> HealthStatus: ...
Loading