from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status, permissions from datetime import datetime, timezone # Import Service Layer from api.services.health_service import HealthService # Dependency Injection: สร้าง Instance ของ Service health_service = HealthService() 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 )