|
| 1 | +import logging |
| 2 | +import traceback |
| 3 | + |
| 4 | +from django.http import HttpResponse, JsonResponse |
| 5 | +from django.utils import timezone |
| 6 | +from rest_framework.views import APIView |
| 7 | +from rest_framework.response import Response |
| 8 | +from rest_framework import status |
| 9 | +from django.conf import settings |
| 10 | +from sapl.health import check_app, check_db, check_cache |
| 11 | + |
| 12 | +COMMON_HEADERS = { |
| 13 | + "Cache-Control": "no-store, no-cache, must-revalidate, max-age=0", |
| 14 | + "Pragma": "no-cache", |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +def _format_plain(ok: bool) -> HttpResponse: |
| 19 | + return HttpResponse("OK\n" if ok else "UNHEALTHY\n", |
| 20 | + status=status.HTTP_200_OK if ok else status.HTTP_503_SERVICE_UNAVAILABLE, |
| 21 | + content_type="text/plain") |
| 22 | + |
| 23 | + |
| 24 | +class HealthzView(APIView): |
| 25 | + authentication_classes = [] |
| 26 | + permission_classes = [] |
| 27 | + |
| 28 | + logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | + def get(self, request): |
| 31 | + try: |
| 32 | + ok, msg, ms = check_app() |
| 33 | + payload = { |
| 34 | + "status": "OK" if ok else "UNHEALTHY", |
| 35 | + "checks": {"app": {"ok": ok, "latency_ms": round(ms, 1), "error": msg}}, |
| 36 | + "version": settings.SAPL_VERSION, |
| 37 | + "time": timezone.now().isoformat(), |
| 38 | + } |
| 39 | + if request.query_params.get("fmt") == "txt": |
| 40 | + return _format_plain(ok) |
| 41 | + return Response(payload, |
| 42 | + status=status.HTTP_200_OK if ok else status.HTTP_503_SERVICE_UNAVAILABLE, |
| 43 | + headers=COMMON_HEADERS) |
| 44 | + except Exception as e: |
| 45 | + self.logger.error(traceback.format_exc()) |
| 46 | + return "An internal error has occurred!" |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +class ReadyzView(APIView): |
| 51 | + authentication_classes = [] |
| 52 | + permission_classes = [] |
| 53 | + |
| 54 | + logger = logging.getLogger(__name__) |
| 55 | + |
| 56 | + def get(self, request): |
| 57 | + try: |
| 58 | + checks = { |
| 59 | + "app": check_app(), |
| 60 | + "db": check_db(), |
| 61 | + "cache": check_cache(), |
| 62 | + } |
| 63 | + payload_checks = { |
| 64 | + name: {"ok": r[0], "latency_ms": round(r[2], 1), "error": r[1]} |
| 65 | + for name, r in checks.items() |
| 66 | + } |
| 67 | + ok = all(r[0] for r in checks.values()) |
| 68 | + payload = { |
| 69 | + "status": "ok" if ok else "unhealthy", |
| 70 | + "checks": payload_checks, |
| 71 | + "version": settings.SAPL_VERSION, |
| 72 | + "time": timezone.now().isoformat(), |
| 73 | + } |
| 74 | + if request.query_params.get("fmt") == "txt": |
| 75 | + return _format_plain(ok) |
| 76 | + return Response(payload, |
| 77 | + status=status.HTTP_200_OK if ok else status.HTTP_503_SERVICE_UNAVAILABLE, |
| 78 | + headers=COMMON_HEADERS) |
| 79 | + except Exception as e: |
| 80 | + self.logger.error(traceback.format_exc()) |
| 81 | + return "An internal error has occurred!" |
| 82 | + |
| 83 | + |
| 84 | +class AppzVersionView(APIView): |
| 85 | + authentication_classes = [] |
| 86 | + permission_classes = [] |
| 87 | + |
| 88 | + logger = logging.getLogger(__name__) |
| 89 | + |
| 90 | + def get(self, request): |
| 91 | + try: |
| 92 | + payload = { |
| 93 | + 'name': 'SAPL', |
| 94 | + 'description': 'Sistema de Apoio ao Processo Legislativo', |
| 95 | + 'version': settings.SAPL_VERSION, |
| 96 | + } |
| 97 | + if request.query_params.get("fmt") == "txt": |
| 98 | + return HttpResponse(f"{payload['version']} {payload['name']}", |
| 99 | + status=status.HTTP_200_OK, |
| 100 | + content_type="text/plain") |
| 101 | + return Response(payload, |
| 102 | + status=status.HTTP_200_OK, |
| 103 | + headers=COMMON_HEADERS) |
| 104 | + except Exception as e: |
| 105 | + self.logger.error(traceback.format_exc()) |
| 106 | + return "An internal error has occurred!" |
0 commit comments