Skip to content

Commit

Permalink
Changed health endpoint response schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmoy12c committed Apr 14, 2023
1 parent 6f4d2e5 commit 5aac743
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 29 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/uci/outbound/health/HealthController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.uci.outbound.health;

import com.fasterxml.jackson.databind.JsonNode;
import com.uci.dao.service.HealthService;
import com.uci.utils.model.ApiResponse;
import com.uci.utils.model.ApiResponseParams;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Status;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -25,9 +27,17 @@ public Mono<ResponseEntity<ApiResponse>> statusCheck() {
return healthService.getAllHealthNode().map(health -> ApiResponse.builder()
.id("api.health")
.params(ApiResponseParams.builder().build())
.responseCode(HttpStatus.OK.name())
.result(health)
.build()
).map(ResponseEntity::ok);
).map(response -> {
if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) {
response.responseCode = HttpStatus.OK.name();
return new ResponseEntity<>(response, HttpStatus.OK);
}
else {
response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name();
return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
}
});
}
}
79 changes: 52 additions & 27 deletions src/main/java/com/uci/outbound/health/ServiceStatusController.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package com.uci.outbound.health;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.uci.dao.service.HealthService;

import com.uci.utils.model.ApiResponse;
import com.uci.utils.model.ApiResponseParams;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Status;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -30,23 +26,28 @@ public class ServiceStatusController {
private HealthService healthService;

/**
* In use by sunbird team - to check service liveliness & readliness
* @return
* @throws JsonProcessingException
*/
* In use by sunbird team - to check service liveliness & readliness
*
* @return
* @throws JsonProcessingException
*/
@RequestMapping(value = "/health", method = RequestMethod.GET, produces = { "application/json", "text/json" })
public ResponseEntity<ApiResponse> statusCheck() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
JsonNode resultNode = mapper.readTree("{\"healthy\":true}");

ApiResponse response = ApiResponse.builder()
.id("api.service.health.cassandra")
.params(ApiResponseParams.builder().build())
.responseCode(HttpStatus.OK.name())
.result(resultNode)
.build();

return ResponseEntity.ok(response);
public Mono<ResponseEntity<ApiResponse>> statusCheck() throws JsonProcessingException {
return healthService.getAllHealthNode().map(health -> ApiResponse.builder()
.id("api.health")
.params(ApiResponseParams.builder().build())
.result(health)
.build()
).map(response -> {
if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) {
response.responseCode = HttpStatus.OK.name();
return new ResponseEntity<>(response, HttpStatus.OK);
}
else {
response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name();
return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
}
});
}

@RequestMapping(value = "/health/cassandra", method = RequestMethod.GET, produces = { "application/json", "text/json" })
Expand All @@ -55,10 +56,18 @@ public Mono<ResponseEntity<ApiResponse>> cassandraStatusCheck() {
ApiResponse.builder()
.id("api.service.health.cassandra")
.params(ApiResponseParams.builder().build())
.responseCode(HttpStatus.OK.name())
.result(result)
.build())
.map(ResponseEntity::ok);
.map(response -> {
if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) {
response.responseCode = HttpStatus.OK.name();
return new ResponseEntity<>(response, HttpStatus.OK);
}
else {
response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name();
return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
}
});
}

@RequestMapping(value = "/health/kafka", method = RequestMethod.GET, produces = { "application/json", "text/json" })
Expand All @@ -67,20 +76,36 @@ public Mono<ResponseEntity<ApiResponse>> kafkaStatusCheck() {
ApiResponse.builder()
.id("api.service.health.kafka")
.params(ApiResponseParams.builder().build())
.responseCode(HttpStatus.OK.name())
.result(result)
.build())
.map(ResponseEntity::ok);
.map(response -> {
if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) {
response.responseCode = HttpStatus.OK.name();
return new ResponseEntity<>(response, HttpStatus.OK);
}
else {
response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name();
return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
}
});
}

@RequestMapping(value = "/health/campaign", method = RequestMethod.GET, produces = { "application/json", "text/json" })
public Mono<ResponseEntity<ApiResponse>> campaignUrlStatusCheck() {
return healthService.getCampaignUrlHealthNode().map(result ->
ApiResponse.builder().id("api.service.health.campaign")
.params(ApiResponseParams.builder().build())
.responseCode(HttpStatus.OK.name())
.result(result)
.build())
.map(ResponseEntity::ok);
.map(response -> {
if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) {
response.responseCode = HttpStatus.OK.name();
return new ResponseEntity<>(response, HttpStatus.OK);
}
else {
response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name();
return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
}
});
}
}

0 comments on commit 5aac743

Please sign in to comment.