forked from PSMRI/FHIR-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealthController.java
More file actions
62 lines (50 loc) · 2.57 KB
/
HealthController.java
File metadata and controls
62 lines (50 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.wipro.fhir.controller.health;
import java.time.Instant;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wipro.fhir.service.health.HealthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
@RestController
@RequestMapping("/health")
@Tag(name = "Health Check", description = "APIs for checking infrastructure health status")
public class HealthController {
private static final Logger logger = LoggerFactory.getLogger(HealthController.class);
private final HealthService healthService;
public HealthController(HealthService healthService) {
this.healthService = healthService;
}
@GetMapping
@Operation(summary = "Check infrastructure health",
description = "Returns the health status of MySQL, Redis, and other configured services")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Services are UP or DEGRADED (operational with warnings)"),
@ApiResponse(responseCode = "503", description = "One or more critical services are DOWN")
})
public ResponseEntity<Map<String, Object>> checkHealth() {
logger.info("Health check endpoint called");
try {
Map<String, Object> healthStatus = healthService.checkHealth();
String overallStatus = (String) healthStatus.get("status");
// Return 503 only if DOWN; 200 for both UP and DEGRADED (DEGRADED = operational with warnings)
HttpStatus httpStatus = "DOWN".equals(overallStatus) ? HttpStatus.SERVICE_UNAVAILABLE : HttpStatus.OK;
logger.debug("Health check completed with status: {}", overallStatus);
return new ResponseEntity<>(healthStatus, httpStatus);
} catch (Exception e) {
logger.error("Unexpected error during health check", e);
Map<String, Object> errorResponse = Map.of(
"status", "DOWN",
"timestamp", Instant.now().toString()
);
return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE);
}
}
}