Skip to content

Commit

Permalink
feat: implement /healthcheck endpoint
Browse files Browse the repository at this point in the history
implements the healthcheck as a middleware before the HOST_HEADER verification (in django.middleware.common.CommonMiddleware), so healthcheck requests always pass

based on https://stackoverflow.com/a/64623669
  • Loading branch information
lalver1 committed Dec 9, 2024
1 parent 3cc70ff commit a4a275d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
Empty file added pems/core/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions pems/core/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
The core application: middleware definitions for request/response cycle.
"""

from django.http import HttpResponse

HEALTHCHECK_PATH = "/healthcheck"


class Healthcheck:
"""Middleware intercepts and accepts /healthcheck requests."""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
if request.path == HEALTHCHECK_PATH:
return HttpResponse("Healthy", content_type="text/plain")
return self.get_response(request)
1 change: 1 addition & 0 deletions pems/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def _filter_empty(ls):
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"pems.core.middleware.Healthcheck",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
Expand Down
Empty file added tests/pytest/core/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions tests/pytest/core/test_middleware_healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pems.core.middleware import HEALTHCHECK_PATH


def test_healthcheck(client):
response = client.get(HEALTHCHECK_PATH)
assert response.status_code == 200

0 comments on commit a4a275d

Please sign in to comment.