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
1 change: 0 additions & 1 deletion {{ cookiecutter.name }}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ dependencies = [
"django-axes>=8.3.1",
"django-environ>=0.14.0",
"django-filter>=26.1",
"django-healthchecks>=1.5.0",
"django-ipware>=7.0.1",
"django-split-settings>=1.3.2",
"django-storages>=1.14.6",
Expand Down
23 changes: 23 additions & 0 deletions {{ cookiecutter.name }}/src/app/api/healthchecks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.db import DatabaseError, connection
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView


@extend_schema(exclude=True) # the endpoint serves docker and load balancers, not clients generating code
@method_decorator(never_cache, name="dispatch")
class HealthCheckView(APIView):
authentication_classes = [] # token auth queries the database, so a dead one would 500 before the check below runs

def get(self, request: Request) -> Response:
try:
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
except DatabaseError:
return Response({"db": False}, status=status.HTTP_503_SERVICE_UNAVAILABLE)

return Response({"db": True})
7 changes: 0 additions & 7 deletions {{ cookiecutter.name }}/src/app/conf/healthchecks.py

This file was deleted.

1 change: 0 additions & 1 deletion {{ cookiecutter.name }}/src/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"conf/boilerplate.py",
"conf/db.py",
"conf/celery.py",
"conf/healthchecks.py",
"conf/http.py",
"conf/i18n.py",
"conf/installed_apps.py",
Expand Down
2 changes: 1 addition & 1 deletion {{ cookiecutter.name }}/src/app/testing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ApiClient(DRFAPIClient):
Client is available as two fixtures: `as_anon` and `as_user`. Use it like this:

def test(as_anon):
as_anon.get("/api/v1/healthchecks/db/") # fetch endpoint anonymously
as_anon.get("/api/v1/healthchecks/") # fetch endpoint anonymously


def test_whoami(as_user, user):
Expand Down
14 changes: 11 additions & 3 deletions {{ cookiecutter.name }}/src/app/tests/test_health.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import pytest
from django.db import OperationalError


pytestmark = [
pytest.mark.django_db,
pytest.mark.filterwarnings("ignore:.*inspect.getargspec().*:DeprecationWarning"),
]


def test(as_anon):
result = as_anon.get("/api/v1/healthchecks/db/")
result = as_anon.get("/api/v1/healthchecks/")

assert result == "true"
assert result == {"db": True}


def test_503_when_database_is_down(as_anon, mocker):
mocker.patch("django.db.connection.cursor", side_effect=OperationalError) # connection.close() is a no-op on in-memory SQLite

result = as_anon.get("/api/v1/healthchecks/", expected_status=503)

assert result == {"db": False}
4 changes: 3 additions & 1 deletion {{ cookiecutter.name }}/src/app/urls/v1.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from django.urls import include, path
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView

from app.api.healthchecks import HealthCheckView


app_name = "api_v1"

urlpatterns = [
path("auth/", include("a12n.api.urls")),
path("users/", include("users.api.urls")),
path("healthchecks/", include("django_healthchecks.urls")),
path("healthchecks/", HealthCheckView.as_view(), name="healthchecks"),
path("docs/schema/", SpectacularAPIView.as_view(), name="schema"),
path("docs/swagger/", SpectacularSwaggerView.as_view(url_name="schema")),
]
71 changes: 0 additions & 71 deletions {{ cookiecutter.name }}/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading