help-desk/backend/api/views/health_check_view.py
2025-12-01 05:43:37 +07:00

37 lines
1.4 KiB
Python

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status, permissions
from drf_spectacular.utils import extend_schema
# Import Service Layer
from api.services.health_service import HealthService
# Dependency Injection: สร้าง Instance ของ Service
health_service = HealthService()
@extend_schema(tags=['2. Application Service'])
class SystemHealthCheck(APIView):
"""
GET /api/v1/health/
Controller สำหรับดึงสถานะ Health Check ของระบบ
"""
permission_classes = [permissions.AllowAny]
def get(self, request):
try:
# เรียกใช้ Service Layer
response_data = health_service.get_system_health()
# กำหนด HTTP Status ตามสถานะรวม
http_status = status.HTTP_200_OK
if response_data['status'] != "Healthy":
http_status = status.HTTP_503_SERVICE_UNAVAILABLE
return Response(response_data, status=http_status)
except Exception as e:
# จัดการข้อผิดพลาดที่ไม่คาดคิดในระดับ View
return Response(
{"status": "Error", "detail": f"Internal Server Error during health check: {e}"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR
)