Skip to content

Commit 50bbaa0

Browse files
committed
fix: changes for /health and /version endpoints
1 parent e71943a commit 50bbaa0

7 files changed

Lines changed: 431 additions & 240 deletions

File tree

pom.xml

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -295,26 +295,25 @@
295295
<plugin>
296296
<groupId>pl.project13.maven</groupId>
297297
<artifactId>git-commit-id-plugin</artifactId>
298-
<version>2.2.4</version>
298+
<version>4.9.10</version>
299299
<executions>
300300
<execution>
301301
<id>get-the-git-infos</id>
302302
<goals>
303303
<goal>revision</goal>
304304
</goals>
305+
<phase>initialize</phase>
305306
</execution>
306307
</executions>
307308
<configuration>
308-
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
309-
<prefix>git</prefix>
310-
<verbose>false</verbose>
311309
<generateGitPropertiesFile>true</generateGitPropertiesFile>
312-
<generateGitPropertiesFilename>
313-
${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
314-
<format>json</format>
315-
<gitDescribe>
316-
<skip>true</skip>
317-
</gitDescribe>
310+
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
311+
<includeOnlyProperties>
312+
<includeOnlyProperty>git.commit.id</includeOnlyProperty>
313+
<includeOnlyProperty>git.build.time</includeOnlyProperty>
314+
</includeOnlyProperties>
315+
<commitIdGenerationMode>full</commitIdGenerationMode>
316+
<format>properties</format>
318317
</configuration>
319318
</plugin>
320319
<plugin>
@@ -462,50 +461,20 @@
462461
</execution>
463462
</executions>
464463
</plugin>
465-
<!-- add the git-commit-id-plugin configuration -->
466-
<plugin>
467-
<groupId>pl.project13.maven</groupId>
468-
<artifactId>git-commit-id-plugin</artifactId>
469-
<version>4.9.10</version>
470-
<executions>
471-
<execution>
472-
<id>get-the-git-infos</id>
473-
<goals>
474-
<goal>revision</goal>
475-
</goals>
476-
<phase>initialize</phase>
477-
</execution>
478-
</executions>
479-
<configuration>
480-
<generateGitPropertiesFile>true</generateGitPropertiesFile>
481-
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
482-
<includeOnlyProperties>
483-
<includeOnlyProperty>git.commit.id</includeOnlyProperty>
484-
<includeOnlyProperty>git.build.time</includeOnlyProperty>
485-
</includeOnlyProperties>
486-
<commitIdGenerationMode>full</commitIdGenerationMode>
487-
</configuration>
488-
</plugin>
489-
<!-- Add Spring Boot Maven plugin to generate build info -->
490464
<plugin>
491465
<groupId>org.springframework.boot</groupId>
492466
<artifactId>spring-boot-maven-plugin</artifactId>
467+
<version>3.2.2</version>
493468
<executions>
494469
<execution>
495470
<goals>
496-
<goal>build-info</goal>
471+
<goal>repackage</goal>
497472
</goals>
498473
</execution>
499-
</executions>
500-
</plugin>
501-
<plugin>
502-
<groupId>org.springframework.boot</groupId>
503-
<artifactId>spring-boot-maven-plugin</artifactId>
504-
<version>3.2.2</version>
505-
<executions>
506474
<execution>
475+
<id>build-info</id>
507476
<goals>
508-
<goal>repackage</goal>
477+
<goal>build-info</goal>
509478
</goals>
510479
</execution>
511480
</executions>
@@ -521,4 +490,4 @@
521490
</plugin>
522491
</plugins>
523492
</reporting>
524-
</project>
493+
</project>

src/main/java/com/iemr/admin/config/SecurityFilterConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ public FilterRegistrationBean<JwtUserIdValidationFilter> jwtFilterRegistration()
2323
FilterRegistrationBean<JwtUserIdValidationFilter> registration = new FilterRegistrationBean<>();
2424
registration.setFilter(new JwtUserIdValidationFilter(jwtAuthenticationUtil, allowedOrigins));
2525
registration.addUrlPatterns("/*");
26+
registration.setOrder(1);
2627

27-
// Exclude health and version endpoints
28-
registration.addInitParameter("excludedUrls", "/health,/version");
28+
// Set name for easier debugging
29+
registration.setName("JwtUserIdValidationFilter");
2930

3031
return registration;
3132
}

src/main/java/com/iemr/admin/controller/health/HealthController.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,42 @@
1919
* You should have received a copy of the GNU General Public License
2020
* along with this program. If not, see https://www.gnu.org/licenses/.
2121
*/
22-
package com.iemr.admin.controller.healthcheck;
22+
package com.iemr.admin.controller.health;
2323

24+
import java.util.Map;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.http.HttpStatus;
2430
import org.springframework.http.ResponseEntity;
2531
import org.springframework.web.bind.annotation.GetMapping;
2632
import org.springframework.web.bind.annotation.RestController;
2733

28-
import java.util.HashMap;
29-
import java.util.Map;
34+
import com.iemr.admin.service.health.HealthService;
35+
36+
import io.swagger.v3.oas.annotations.Operation;
3037

3138
@RestController
3239
public class HealthController {
3340

41+
private static final Logger logger = LoggerFactory.getLogger(HealthController.class);
42+
43+
@Autowired
44+
private HealthService healthService;
45+
46+
@Operation(summary = "Health check endpoint")
3447
@GetMapping("/health")
35-
public ResponseEntity<Map<String, String>> health() {
36-
Map<String, String> response = new HashMap<>();
37-
response.put("status", "UP");
38-
return ResponseEntity.ok(response);
48+
public ResponseEntity<Map<String, Object>> health() {
49+
logger.info("Health check endpoint called");
50+
51+
Map<String, Object> healthStatus = healthService.checkHealth();
52+
53+
// Return 503 if any service is down, 200 if all are up
54+
String status = (String) healthStatus.get("status");
55+
HttpStatus httpStatus = "UP".equals(status) ? HttpStatus.OK : HttpStatus.SERVICE_UNAVAILABLE;
56+
57+
logger.info("Health check completed with status: {}", status);
58+
return ResponseEntity.status(httpStatus).body(healthStatus);
3959
}
4060
}

src/main/java/com/iemr/admin/controller/version/VersionController.java

Lines changed: 22 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -21,140 +21,40 @@
2121
*/
2222
package com.iemr.admin.controller.version;
2323

24-
import java.io.IOException;
25-
import java.io.InputStream;
26-
import java.text.SimpleDateFormat;
27-
import java.util.Date;
28-
import java.util.Properties;
29-
import java.util.TimeZone;
30-
3124
import org.slf4j.Logger;
3225
import org.slf4j.LoggerFactory;
26+
import org.springframework.beans.factory.annotation.Autowired;
3327
import org.springframework.http.MediaType;
3428
import org.springframework.web.bind.annotation.RequestMapping;
3529
import org.springframework.web.bind.annotation.RequestMethod;
3630
import org.springframework.web.bind.annotation.RestController;
3731

32+
import com.iemr.admin.service.version.VersionService;
3833
import com.iemr.admin.utils.response.OutputResponse;
3934

4035
import io.swagger.v3.oas.annotations.Operation;
4136

42-
4337
@RestController
4438
public class VersionController {
4539

46-
private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
47-
48-
@Operation(summary = "Version information")
49-
@RequestMapping(value = "/version", method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
50-
public String versionInformation() {
51-
OutputResponse output = new OutputResponse();
52-
try {
53-
logger.info("version Controller Start");
54-
// Set the version information as JSON string directly
55-
output.setResponse(readGitPropertiesAsJson());
56-
} catch (Exception e) {
57-
output.setError(e);
58-
}
59-
60-
logger.info("version Controller End");
61-
62-
// Use standard toString() - no custom formatting
63-
return output.toString();
64-
}
65-
66-
private String readGitPropertiesAsJson() throws Exception {
67-
StringBuilder json = new StringBuilder();
68-
json.append("{\n");
69-
70-
// Read Git properties
71-
Properties gitProps = loadPropertiesFile("git.properties");
72-
if (gitProps != null) {
73-
// For git.commit.id, look for both standard and abbrev versions
74-
String commitId = gitProps.getProperty("git.commit.id", null);
75-
if (commitId == null) {
76-
commitId = gitProps.getProperty("git.commit.id.abbrev", "unknown");
77-
}
78-
json.append(" \"git.commit.id\": \"").append(commitId).append("\",\n");
79-
80-
// For git.build.time, look for various possible property names
81-
String buildTime = gitProps.getProperty("git.build.time", null);
82-
if (buildTime == null) {
83-
buildTime = gitProps.getProperty("git.commit.time", null);
84-
}
85-
if (buildTime == null) {
86-
buildTime = gitProps.getProperty("git.commit.timestamp", "unknown");
87-
}
88-
json.append(" \"git.build.time\": \"").append(buildTime).append("\",\n");
89-
} else {
90-
logger.warn("git.properties file not found. Git information will be unavailable.");
91-
json.append(" \"git.commit.id\": \"information unavailable\",\n");
92-
json.append(" \"git.build.time\": \"information unavailable\",\n");
93-
}
94-
95-
// Read build properties if available
96-
Properties buildProps = loadPropertiesFile("META-INF/build-info.properties");
97-
if (buildProps != null) {
98-
// Extract version - checking for both standard and nested formats
99-
String version = buildProps.getProperty("build.version", null);
100-
if (version == null) {
101-
version = buildProps.getProperty("build.version.number", null);
102-
}
103-
if (version == null) {
104-
version = buildProps.getProperty("version", "unknown");
105-
}
106-
json.append(" \"build.version\": \"").append(version).append("\",\n");
107-
108-
// Extract time - checking for both standard and alternate formats
109-
String time = buildProps.getProperty("build.time", null);
110-
if (time == null) {
111-
time = buildProps.getProperty("build.timestamp", null);
112-
}
113-
if (time == null) {
114-
time = buildProps.getProperty("timestamp", "unknown");
115-
}
116-
json.append(" \"build.time\": \"").append(time).append("\",\n");
117-
} else {
118-
logger.info("build-info.properties not found, trying Maven properties");
119-
// Fallback to maven project version
120-
Properties mavenProps = loadPropertiesFile("META-INF/maven/com.iemr.admin/admin-api/pom.properties");
121-
if (mavenProps != null) {
122-
String version = mavenProps.getProperty("version", "unknown");
123-
json.append(" \"build.version\": \"").append(version).append("\",\n");
124-
json.append(" \"build.time\": \"").append(getCurrentIstTimeFormatted()).append("\",\n");
125-
} else {
126-
logger.warn("Neither build-info.properties nor Maven properties found.");
127-
json.append(" \"build.version\": \"3.1.0\",\n"); // Default version
128-
json.append(" \"build.time\": \"").append(getCurrentIstTimeFormatted()).append("\",\n");
129-
}
130-
}
131-
json.append(" \"current.time\": \"").append(getCurrentIstTimeFormatted()).append("\"\n");
132-
133-
json.append(" }");
134-
return json.toString();
135-
}
136-
137-
/**
138-
* Get the current time formatted in Indian Standard Time (IST)
139-
* IST is UTC+5:30
140-
*/
141-
private String getCurrentIstTimeFormatted() {
142-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
143-
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
144-
return sdf.format(new Date());
145-
}
146-
147-
private Properties loadPropertiesFile(String resourceName) {
148-
ClassLoader classLoader = getClass().getClassLoader();
149-
try (InputStream inputStream = classLoader.getResourceAsStream(resourceName)) {
150-
if (inputStream != null) {
151-
Properties props = new Properties();
152-
props.load(inputStream);
153-
return props;
154-
}
155-
} catch (IOException e) {
156-
logger.warn("Could not load properties file: " + resourceName, e);
157-
}
158-
return null;
159-
}
40+
private static final Logger logger = LoggerFactory.getLogger(VersionController.class);
41+
42+
@Autowired
43+
private VersionService versionService;
44+
45+
@Operation(summary = "Version information")
46+
@RequestMapping(value = "/version", method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
47+
public String versionInformation() {
48+
OutputResponse output = new OutputResponse();
49+
try {
50+
logger.info("version Controller Start");
51+
output.setResponse(versionService.getVersionInformation());
52+
} catch (Exception e) {
53+
logger.error("Error in version controller", e);
54+
output.setError(e);
55+
}
56+
57+
logger.info("version Controller End");
58+
return output.toString();
59+
}
16060
}

0 commit comments

Comments
 (0)