diff --git a/src/main/java/com/example/usermanagement/configuration/MainConfiguration.java b/src/main/java/com/example/usermanagement/configuration/MainConfiguration.java new file mode 100644 index 0000000..910bb63 --- /dev/null +++ b/src/main/java/com/example/usermanagement/configuration/MainConfiguration.java @@ -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(); + } +} diff --git a/src/main/java/com/example/usermanagement/configuration/SecurityConfiguration.java b/src/main/java/com/example/usermanagement/configuration/SecurityConfiguration.java index 5fc00e2..17e79f1 100644 --- a/src/main/java/com/example/usermanagement/configuration/SecurityConfiguration.java +++ b/src/main/java/com/example/usermanagement/configuration/SecurityConfiguration.java @@ -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); diff --git a/src/main/java/com/example/usermanagement/controllers/MainController.java b/src/main/java/com/example/usermanagement/controllers/MainController.java index 05571dc..c8e5299 100644 --- a/src/main/java/com/example/usermanagement/controllers/MainController.java +++ b/src/main/java/com/example/usermanagement/controllers/MainController.java @@ -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 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 health() { + HealthCheckResponse healthCheckResponse = new HealthCheckResponse(); + healthCheckResponse.setStatus("healthy"); + + for (String depServer : depServers) { + try { + ResponseEntity 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); } } diff --git a/src/main/java/com/example/usermanagement/dto/DependencyStatus.java b/src/main/java/com/example/usermanagement/dto/DependencyStatus.java new file mode 100644 index 0000000..b7a67cf --- /dev/null +++ b/src/main/java/com/example/usermanagement/dto/DependencyStatus.java @@ -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; + } +} diff --git a/src/main/java/com/example/usermanagement/dto/HealthCheckResponse.java b/src/main/java/com/example/usermanagement/dto/HealthCheckResponse.java new file mode 100644 index 0000000..a591004 --- /dev/null +++ b/src/main/java/com/example/usermanagement/dto/HealthCheckResponse.java @@ -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 dependencies = new HashMap<>(); +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d8dd048..b5abeaf 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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}