diff --git a/Dockerfile b/Dockerfile index 1a94355..9ecfff7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,6 +43,6 @@ USER app EXPOSE 8000 HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \ - CMD curl -fsS "http://127.0.0.1:${PORT}/" || exit 1 + CMD curl -fsS "http://127.0.0.1:${PORT}/health/" || exit 1 CMD ["/bin/sh", "-c", "exec gunicorn config.wsgi:application --bind 0.0.0.0:${PORT:-8000} --workers ${GUNICORN_WORKERS:-3} --access-logfile - --error-logfile -"] diff --git a/config/urls.py b/config/urls.py index c91fda0..904bbad 100644 --- a/config/urls.py +++ b/config/urls.py @@ -17,7 +17,9 @@ from django.contrib import admin from django.urls import path +from .views import health_check urlpatterns = [ + path("health/", health_check, name="health_check"), path("admin/", admin.site.urls), ] diff --git a/config/views.py b/config/views.py new file mode 100644 index 0000000..90342a0 --- /dev/null +++ b/config/views.py @@ -0,0 +1,21 @@ +from django.http import JsonResponse +from django.db import connection +from django.views.decorators.http import require_GET +import logging + +logger = logging.getLogger(__name__) + + +@require_GET +def health_check(request): + try: + with connection.cursor() as cursor: + cursor.execute("SELECT 1") + cursor.fetchone() + + return JsonResponse({"status": "healthy", "database": "connected"}) + except Exception as e: + logger.error("Health check failed: %s", str(e), exc_info=True) + return JsonResponse( + {"status": "unhealthy", "database": "disconnected"}, status=503 + ) diff --git a/docker-compose.yml b/docker-compose.yml index 8a962bf..e986b05 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,7 +27,7 @@ services: volumes: - static-data:/app/static healthcheck: - test: ["CMD", "curl", "-fsSL", "http://127.0.0.1:8000/"] + test: ["CMD", "curl", "-fsSL", "http://127.0.0.1:8000/health/"] interval: 30s timeout: 5s retries: 3 diff --git a/main.py b/main.py deleted file mode 100644 index 72e4a44..0000000 --- a/main.py +++ /dev/null @@ -1,6 +0,0 @@ -def main(): - print("Hello from idontneedit!") - - -if __name__ == "__main__": - main()