Skip to content

Commit

Permalink
feat: add health view
Browse files Browse the repository at this point in the history
  • Loading branch information
hartungstenio committed Oct 11, 2024
1 parent 9dd82ed commit d601346
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
6 changes: 6 additions & 0 deletions django_healthy/health_checks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from .base import HealthCheck, HealthCheckResult, HealthStatus
from .service import HealthCheckService, HealthReport, HealthReportEntry

__all__ = [
"HealthCheck",
"HealthCheckResult",
"HealthStatus",
"HealthCheckService",
"HealthReport",
"HealthReportEntry",
]

health_check_service = HealthCheckService()
24 changes: 24 additions & 0 deletions django_healthy/templates/django_healthy/report.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<meta charset="utf-8" />
<title>Health Report</title>
</head>
<body>
<table>
<thead>
<th>System</th>
<th>Status</th>
<th>Time</th>
</thead>
<tbody>
{% for system, reported in report.entries.items %}
<tr>
<td>{{ system }}</td>
<td>{{ reported.status.name }}</td>
<td>{{ reported.duration.total_seconds }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
3 changes: 2 additions & 1 deletion django_healthy/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.urls import path

from .views import LivenessView
from .views import HealthView, LivenessView

app_name = "django_healthy"

urlpatterns = [
path("ping/", LivenessView.as_view(), name="ping"),
path("health/", HealthView.as_view(), name="health"),
]
17 changes: 17 additions & 0 deletions django_healthy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from typing import ClassVar

from django.http import HttpRequest, HttpResponse
from django.template.response import TemplateResponse
from django.views import View

from .health_checks import HealthReport, health_check_service


class LivenessView(View):
http_method_names: ClassVar[list[str]] = [
Expand All @@ -16,3 +19,17 @@ class LivenessView(View):

async def get(self, request: HttpRequest) -> HttpResponse: # noqa: ARG002
return HttpResponse("Pong")


class HealthView(View):
http_method_names: ClassVar[list[str]] = [
"get",
"head",
"options",
"trace",
]

async def get(self, request: HttpRequest) -> HttpResponse:
report: HealthReport = await health_check_service.check_health()
context: dict[str, HealthReport] = {"report": report}
return TemplateResponse(request, "django_healthy/report.html", context)
17 changes: 17 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,20 @@ async def test_methods_not_allowed_async(self, method: str, async_client: AsyncC
response: HttpResponse = await django_client_method(reverse("django_healthy:ping"))

assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED


class TestHealthView:
@pytest.mark.asyncio
async def test_get_async(self, async_client: AsyncClient):
response: HttpResponse = await async_client.get(reverse("django_healthy:health"))

assert response.status_code == HTTPStatus.OK

@pytest.mark.asyncio
@pytest.mark.parametrize("method", ["post", "put", "patch", "delete"])
async def test_methods_not_allowed_async(self, method: str, async_client: AsyncClient):
django_client_method = getattr(async_client, method)

response: HttpResponse = await django_client_method(reverse("django_healthy:health"))

assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED

0 comments on commit d601346

Please sign in to comment.