Add /health/ endpoint and curl for Docker healthchecks

- Add health view checking DB connection at /health/
- Add curl to Dockerfile.prod for container healthcheck support
This commit is contained in:
root 2026-04-08 02:46:40 +02:00
parent c6f7f1da41
commit 4604249834
3 changed files with 26 additions and 1 deletions

View file

@ -14,6 +14,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
netcat-openbsd \
curl \
&& rm -rf /var/lib/apt/lists/*
# Install Python dependencies

View file

@ -4,9 +4,10 @@ from django.contrib import admin
from django.urls import path, include
from drf_spectacular.views import SpectacularAPIView
from locflow.views import swagger_ui
from locflow.views import health, swagger_ui
urlpatterns = [
path('health/', health, name='health'),
path('admin/', admin.site.urls),
path('api/v1/', include('apps.accounts.urls')),
path('api/v1/', include('apps.projects.urls')),

View file

@ -1,6 +1,13 @@
import logging
from django.conf import settings
from django.db import connection
from django.http import JsonResponse
from django.shortcuts import render
from django.urls import reverse
from django.views.decorators.cache import never_cache
logger = logging.getLogger(__name__)
def swagger_ui(request):
@ -8,3 +15,19 @@ def swagger_ui(request):
'schema_url': reverse('schema'),
'umami_website_id': getattr(settings, 'UMAMI_WEBSITE_ID', ''),
})
@never_cache
def health(request):
db_ok = False
try:
connection.ensure_connection()
db_ok = True
except Exception:
logger.warning("Health check: database connection failed")
status_code = 200 if db_ok else 503
return JsonResponse(
{"status": "ok" if db_ok else "degraded", "db": db_ok},
status=status_code,
)