Serve /health and keep 404 scanner noise out of the log file

Something on the network polls /health (and /v1/models) every 30 seconds;
the former now answers 200 with a database check instead of a 404, and
django.request is limited to ERROR in the logging config so scanner 404s
stop filling backend/logs/j621.log. Real server errors still log, and the
dev server keeps printing every request to its console.
This commit is contained in:
2026-09-17 21:50:20 -05:00
parent 487d14c617
commit 38e64140ed
3 changed files with 35 additions and 0 deletions
+20
View File
@@ -1,6 +1,7 @@
import time
from django.conf import settings
from django.db import connection
from rest_framework.exceptions import PermissionDenied
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
@@ -66,3 +67,22 @@ class StatsView(APIView):
"log": stats.log_tail(),
}
)
class HealthView(APIView):
"""Unauthenticated liveness probe for uptime monitors (hits /health)."""
permission_classes = [AllowAny]
authentication_classes = []
def get(self, request):
database = True
try:
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
except Exception: # noqa: BLE001 - report the failure, do not crash
database = False
return Response(
{"status": "ok" if database else "degraded", "database": database},
status=200 if database else 503,
)