|
21 | 21 | */ |
22 | 22 | package com.iemr.tm.controller.version; |
23 | 23 |
|
24 | | -import java.io.BufferedReader; |
25 | 24 | import java.io.IOException; |
26 | 25 | import java.io.InputStream; |
27 | | -import java.io.InputStreamReader; |
| 26 | +import java.util.LinkedHashMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Properties; |
28 | 29 |
|
29 | 30 | import org.slf4j.Logger; |
30 | 31 | import org.slf4j.LoggerFactory; |
31 | 32 |
|
| 33 | +import org.springframework.http.MediaType; |
| 34 | +import org.springframework.http.ResponseEntity; |
32 | 35 | import org.springframework.web.bind.annotation.GetMapping; |
33 | | -import org.springframework.web.bind.annotation.RequestMapping; |
34 | | -import org.springframework.web.bind.annotation.RequestMethod; |
35 | 36 | import org.springframework.web.bind.annotation.RestController; |
36 | 37 |
|
37 | | -import com.iemr.tm.utils.response.OutputResponse; |
38 | | - |
39 | 38 | import io.swagger.v3.oas.annotations.Operation; |
40 | 39 |
|
41 | 40 | @RestController |
42 | 41 | public class VersionController { |
43 | 42 |
|
44 | | - private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); |
| 43 | + private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); |
| 44 | + |
| 45 | + private static final String UNKNOWN_VALUE = "unknown"; |
45 | 46 |
|
46 | 47 | @Operation(summary = "Get version information") |
47 | | - @GetMapping(value = "/version") |
48 | | - public String versionInformation() { |
49 | | - OutputResponse output = new OutputResponse(); |
| 48 | + @GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE) |
| 49 | + public ResponseEntity<Map<String, String>> versionInformation() { |
| 50 | + Map<String, String> response = new LinkedHashMap<>(); |
50 | 51 | try { |
51 | 52 | logger.info("version Controller Start"); |
52 | | - output.setResponse(readGitProperties()); |
| 53 | + Properties gitProperties = loadGitProperties(); |
| 54 | + response.put("buildTimestamp", gitProperties.getProperty("git.build.time", UNKNOWN_VALUE)); |
| 55 | + response.put("version", gitProperties.getProperty("git.build.version", UNKNOWN_VALUE)); |
| 56 | + response.put("branch", gitProperties.getProperty("git.branch", UNKNOWN_VALUE)); |
| 57 | + response.put("commitHash", gitProperties.getProperty("git.commit.id.abbrev", UNKNOWN_VALUE)); |
53 | 58 | } catch (Exception e) { |
54 | | - output.setError(e); |
| 59 | + logger.error("Failed to load version information", e); |
| 60 | + response.put("buildTimestamp", UNKNOWN_VALUE); |
| 61 | + response.put("version", UNKNOWN_VALUE); |
| 62 | + response.put("branch", UNKNOWN_VALUE); |
| 63 | + response.put("commitHash", UNKNOWN_VALUE); |
55 | 64 | } |
56 | | - |
57 | 65 | logger.info("version Controller End"); |
58 | | - return output.toString(); |
59 | | - } |
60 | | - |
61 | | - private String readGitProperties() throws Exception { |
62 | | - ClassLoader classLoader = getClass().getClassLoader(); |
63 | | - InputStream inputStream = classLoader.getResourceAsStream("git.properties"); |
64 | | - |
65 | | - return readFromInputStream(inputStream); |
| 66 | + return ResponseEntity.ok(response); |
66 | 67 | } |
67 | 68 |
|
68 | | - private String readFromInputStream(InputStream inputStream) |
69 | | - throws IOException { |
70 | | - StringBuilder resultStringBuilder = new StringBuilder(); |
71 | | - try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { |
72 | | - String line; |
73 | | - while ((line = br.readLine()) != null) { |
74 | | - resultStringBuilder.append(line).append("\n"); |
| 69 | + private Properties loadGitProperties() throws IOException { |
| 70 | + Properties properties = new Properties(); |
| 71 | + try (InputStream input = getClass().getClassLoader() |
| 72 | + .getResourceAsStream("git.properties")) { |
| 73 | + if (input != null) { |
| 74 | + properties.load(input); |
75 | 75 | } |
76 | 76 | } |
77 | | - return resultStringBuilder.toString(); |
| 77 | + return properties; |
78 | 78 | } |
79 | 79 | } |
0 commit comments