Skip to content

Commit

Permalink
Merge pull request #5 from Wassim-Rached/master
Browse files Browse the repository at this point in the history
improve /api/health
  • Loading branch information
Wassim-Rached authored Oct 3, 2024
2 parents 172eb0f + 2be2f90 commit 1410082
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ CorsConfigurationSource corsConfigurationSource() {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.cors(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorizeRequests ->
authorizeRequests
.requestMatchers(HttpMethod.OPTIONS,"/**").permitAll()
.requestMatchers("/","/api/health").permitAll()
.requestMatchers(HttpMethod.POST, "/api/accounts").permitAll()
.anyRequest().authenticated()
);
http
.cors(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorizeRequests ->
authorizeRequests
.requestMatchers(HttpMethod.OPTIONS,"/**").permitAll()
.requestMatchers("/","/api/health").permitAll()
.requestMatchers(HttpMethod.POST, "/api/accounts").permitAll()
.anyRequest().authenticated()
);

http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

Expand Down
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 src/main/java/com/example/usermanagement/dto/DependencyStatus.java
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;
}
}
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<>();
}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ app:
EXPOSED-HEADERS: ${CORS_EXPOSED_HEADERS:*}
ALLOW-CREDENTIALS: ${CORS_ALLOW_CREDENTIALS:true}
MAX-AGE: ${CORS_MAX_AGE:3600}
depServers: ${DEP_SERVERS:http://localhost:3002}

0 comments on commit 1410082

Please sign in to comment.