Skip to content
17 changes: 9 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,10 @@
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>

<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.9.10</version>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>9.0.2</version>
<executions>
<execution>
<id>get-the-git-infos</id>
Expand All @@ -314,11 +313,13 @@
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id</includeOnlyProperty>
<includeOnlyProperty>git.build.time</includeOnlyProperty>
<property>^git.branch$</property>
<property>^git.commit.id.abbrev$</property>
<property>^git.build.version$</property>
<property>^git.build.time$</property>
</includeOnlyProperties>
<commitIdGenerationMode>full</commitIdGenerationMode>
<format>properties</format>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,66 @@
*/
package com.iemr.admin.controller.health;

import java.time.Instant;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.iemr.admin.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);

@Autowired
private HealthService healthService;
private final HealthService healthService;

public HealthController(HealthService healthService) {
this.healthService = healthService;
}

@Operation(summary = "Health check endpoint")
@GetMapping("/health")
public ResponseEntity<Map<String, Object>> health() {
logger.info("Health check endpoint called");

Map<String, Object> healthStatus = healthService.checkHealth();
@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.debug("Health check endpoint called");

// Return 503 if any service is down, 200 if all are up
String status = (String) healthStatus.get("status");
HttpStatus httpStatus = "UP".equals(status) ? HttpStatus.OK : HttpStatus.SERVICE_UNAVAILABLE;

logger.info("Health check completed with status: {}", status);
return ResponseEntity.status(httpStatus).body(healthStatus);
try {
Map<String, Object> healthStatus = healthService.checkHealth();
String overallStatus = (String) healthStatus.get("status");

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);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,59 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

package com.iemr.admin.controller.version;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iemr.admin.service.version.VersionService;

import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Properties;
import org.springframework.http.MediaType;

@RestController
public class VersionController {

private static final Logger logger = LoggerFactory.getLogger(VersionController.class);
private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

private static final String UNKNOWN_VALUE = "unknown";

@Autowired
private VersionService versionService;
@Operation(summary = "Get version information")
@GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> versionInformation() {
Map<String, String> response = new LinkedHashMap<>();
try {
logger.info("version Controller Start");
Properties gitProperties = loadGitProperties();
response.put("buildTimestamp", gitProperties.getProperty("git.build.time", UNKNOWN_VALUE));
response.put("version", gitProperties.getProperty("git.build.version", UNKNOWN_VALUE));
response.put("branch", gitProperties.getProperty("git.branch", UNKNOWN_VALUE));
response.put("commitHash", gitProperties.getProperty("git.commit.id.abbrev", UNKNOWN_VALUE));
} catch (Exception e) {
logger.error("Failed to load version information", e);
response.put("buildTimestamp", UNKNOWN_VALUE);
response.put("version", UNKNOWN_VALUE);
response.put("branch", UNKNOWN_VALUE);
response.put("commitHash", UNKNOWN_VALUE);
}
logger.info("version Controller End");
return ResponseEntity.ok(response);
}

@Operation(summary = "Version information")
@GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, String> versionInformation() {
logger.info("version Controller Start");
Map<String, String> versionInfo = versionService.getVersionInfo();
logger.info("version Controller End");
return versionInfo;
}
private Properties loadGitProperties() throws IOException {
Properties properties = new Properties();
try (InputStream input = getClass().getClassLoader()
.getResourceAsStream("git.properties")) {
if (input != null) {
properties.load(input);
}
}
return properties;
}
}
Loading
Loading