-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Wassim-Rached/master
improve /api/health
- Loading branch information
Showing
6 changed files
with
94 additions
and
12 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/usermanagement/configuration/MainConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.usermanagement.configuration; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class MainConfiguration { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 36 additions & 2 deletions
38
src/main/java/com/example/usermanagement/controllers/MainController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,51 @@ | ||
package com.example.usermanagement.controllers; | ||
|
||
import com.example.usermanagement.dto.DependencyStatus; | ||
import com.example.usermanagement.dto.HealthCheckResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class MainController { | ||
|
||
@Value("${app.depServers}") | ||
private List<String> depServers; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
@GetMapping("/") | ||
public String home() { | ||
return "Identity and Access Control Service Is Up and Running"; | ||
} | ||
|
||
@GetMapping("/api/health") | ||
public String health() { | ||
return "Service is healthy"; | ||
public ResponseEntity<HealthCheckResponse> health() { | ||
HealthCheckResponse healthCheckResponse = new HealthCheckResponse(); | ||
healthCheckResponse.setStatus("healthy"); | ||
|
||
for (String depServer : depServers) { | ||
try { | ||
ResponseEntity<Map> response = restTemplate.getForEntity(depServer + "/api/health", Map.class); | ||
response.getStatusCode().is2xxSuccessful(); // Raise an exception for error status codes | ||
healthCheckResponse.getDependencies().put(depServer, new DependencyStatus("healthy", response.getStatusCode().value())); | ||
} catch (Exception e) { | ||
healthCheckResponse.setStatus("unhealthy"); | ||
healthCheckResponse.getDependencies().put(depServer, new DependencyStatus("unhealthy", 500, e.getMessage())); | ||
} | ||
} | ||
|
||
HttpStatus statusCode = healthCheckResponse.getStatus().equals("healthy") ? HttpStatus.OK : HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
return new ResponseEntity<>(healthCheckResponse, statusCode); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/example/usermanagement/dto/DependencyStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.usermanagement.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
public class DependencyStatus { | ||
private String status; | ||
private Integer code; | ||
private String message; | ||
|
||
public DependencyStatus(String status, Integer code) { | ||
this.status = status; | ||
this.code = code; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/usermanagement/dto/HealthCheckResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.usermanagement.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Getter | ||
@Setter | ||
public class HealthCheckResponse { | ||
private String status; | ||
private Map<String, DependencyStatus> dependencies = new HashMap<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters