chore: initial commit
Signed-off-by: Alan Brault <alan.brault@visus.io>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package io.visus.demos.kotlinapi.controller
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation
|
||||
import io.swagger.v3.oas.annotations.media.Content
|
||||
import io.swagger.v3.oas.annotations.media.Schema
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse
|
||||
import io.swagger.v3.oas.annotations.tags.Tag
|
||||
import io.visus.demos.kotlinapi.api.dto.HealthResponse
|
||||
import io.visus.demos.kotlinapi.domain.model.HealthStatus
|
||||
import io.visus.demos.kotlinapi.domain.model.Status
|
||||
import io.visus.demos.kotlinapi.domain.service.HealthService
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@Tag(name = "Health", description = "Health check endpoints")
|
||||
class HealthController(
|
||||
private val healthCheckServices: List<HealthService>,
|
||||
) {
|
||||
@GetMapping("/health", produces = [MediaType.APPLICATION_JSON_VALUE])
|
||||
@Operation(
|
||||
summary = "Health check",
|
||||
description = "Returns the health status of the API",
|
||||
responses = [
|
||||
ApiResponse(
|
||||
responseCode = "200",
|
||||
description = "API is healthy",
|
||||
content = [
|
||||
Content(
|
||||
mediaType = MediaType.APPLICATION_JSON_VALUE,
|
||||
schema = Schema(implementation = HealthResponse::class),
|
||||
),
|
||||
],
|
||||
),
|
||||
ApiResponse(
|
||||
responseCode = "503",
|
||||
description = "API is down or degraded",
|
||||
content = [
|
||||
Content(
|
||||
mediaType = MediaType.APPLICATION_JSON_VALUE,
|
||||
schema = Schema(implementation = HealthResponse::class),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
fun health(): ResponseEntity<HealthResponse> {
|
||||
val componentHealths = healthCheckServices.map { it.check() }
|
||||
val healthStatus = HealthStatus.fromComponents(componentHealths)
|
||||
val response = HealthResponse.from(healthStatus)
|
||||
|
||||
val httpStatus =
|
||||
when (healthStatus.status) {
|
||||
Status.UP -> HttpStatus.OK
|
||||
Status.DOWN, Status.DEGRADED -> HttpStatus.SERVICE_UNAVAILABLE
|
||||
}
|
||||
|
||||
return ResponseEntity.status(httpStatus).body(response)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user