|
| 1 | +/* |
| 2 | +* AMRIT – Accessible Medical Records via Integrated Technology |
| 3 | +* Integrated EHR (Electronic Health Records) Solution |
| 4 | +* |
| 5 | +* Copyright (C) "Piramal Swasthya Management and Research Institute" |
| 6 | +* |
| 7 | +* This file is part of AMRIT. |
| 8 | +* |
| 9 | +* This program is free software: you can redistribute it and/or modify |
| 10 | +* it under the terms of the GNU General Public License as published by |
| 11 | +* the Free Software Foundation, either version 3 of the License, or |
| 12 | +* (at your option) any later version. |
| 13 | +* |
| 14 | +* This program is distributed in the hope that it will be useful, |
| 15 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +* GNU General Public License for more details. |
| 18 | +* |
| 19 | +* You should have received a copy of the GNU General Public License |
| 20 | +* along with this program. If not, see https://www.gnu.org/licenses/. |
| 21 | +*/ |
| 22 | +package com.iemr.admin.controller.health; |
| 23 | + |
| 24 | +import java.time.Instant; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | +import org.springframework.http.HttpStatus; |
| 30 | +import org.springframework.http.ResponseEntity; |
| 31 | +import org.springframework.web.bind.annotation.GetMapping; |
| 32 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 33 | +import org.springframework.web.bind.annotation.RestController; |
| 34 | + |
| 35 | +import com.iemr.admin.service.health.HealthService; |
| 36 | + |
| 37 | +import io.swagger.v3.oas.annotations.Operation; |
| 38 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 39 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 40 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 41 | + |
| 42 | +@RestController |
| 43 | +@RequestMapping("/health") |
| 44 | +@Tag(name = "Health Check", description = "APIs for checking infrastructure health status") |
| 45 | +public class HealthController { |
| 46 | + |
| 47 | + private static final Logger logger = LoggerFactory.getLogger(HealthController.class); |
| 48 | + |
| 49 | + private final HealthService healthService; |
| 50 | + |
| 51 | + public HealthController(HealthService healthService) { |
| 52 | + this.healthService = healthService; |
| 53 | + } |
| 54 | + |
| 55 | + @GetMapping |
| 56 | + @Operation(summary = "Check infrastructure health", |
| 57 | + description = "Returns the health status of MySQL, Redis, and other configured services") |
| 58 | + @ApiResponses({ |
| 59 | + @ApiResponse(responseCode = "200", description = "Services are UP or DEGRADED (operational with warnings)"), |
| 60 | + @ApiResponse(responseCode = "503", description = "One or more critical services are DOWN") |
| 61 | + }) |
| 62 | + public ResponseEntity<Map<String, Object>> checkHealth() { |
| 63 | + logger.debug("Health check endpoint called"); |
| 64 | + |
| 65 | + try { |
| 66 | + Map<String, Object> healthStatus = healthService.checkHealth(); |
| 67 | + String overallStatus = (String) healthStatus.get("status"); |
| 68 | + |
| 69 | + HttpStatus httpStatus = "DOWN".equals(overallStatus) ? HttpStatus.SERVICE_UNAVAILABLE : HttpStatus.OK; |
| 70 | + |
| 71 | + logger.debug("Health check completed with status: {}", overallStatus); |
| 72 | + return new ResponseEntity<>(healthStatus, httpStatus); |
| 73 | + |
| 74 | + } catch (Exception e) { |
| 75 | + logger.error("Unexpected error during health check", e); |
| 76 | + |
| 77 | + Map<String, Object> errorResponse = Map.of( |
| 78 | + "status", "DOWN", |
| 79 | + "timestamp", Instant.now().toString() |
| 80 | + ); |
| 81 | + |
| 82 | + return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments