refactor: leverage mappie
Signed-off-by: Alan Brault <alan.brault@visus.io>
This commit is contained in:
@@ -4,6 +4,7 @@ plugins {
|
||||
id("org.springframework.boot") version "4.1.0-M2"
|
||||
id("io.spring.dependency-management") version "1.1.7"
|
||||
id("org.jlleitschuh.gradle.ktlint") version "12.1.0"
|
||||
id("tech.mappie.plugin") version "2.2.21-2.1.1"
|
||||
}
|
||||
|
||||
group = "io.visus.demos"
|
||||
@@ -43,6 +44,7 @@ dependencies {
|
||||
implementation("org.springframework.boot:spring-boot-starter-security")
|
||||
implementation("org.springframework.boot:spring-boot-starter-webmvc")
|
||||
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
||||
implementation("tech.mappie:mappie-api:2.2.21-2.1.1")
|
||||
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.4")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-mongodb-test")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-restclient-test")
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.visus.demos.kotlinapi.api.dto
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema
|
||||
import io.visus.demos.kotlinapi.domain.model.ComponentHealth
|
||||
|
||||
@Schema(description = "Health status of an individual component")
|
||||
data class ComponentHealthDto(
|
||||
@@ -9,12 +8,4 @@ data class ComponentHealthDto(
|
||||
val status: String,
|
||||
@Schema(description = "Optional message with additional details", example = "MongoDB connection is active")
|
||||
val message: String? = null,
|
||||
) {
|
||||
companion object {
|
||||
fun from(componentHealth: ComponentHealth): ComponentHealthDto =
|
||||
ComponentHealthDto(
|
||||
status = componentHealth.status.name,
|
||||
message = componentHealth.message,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package io.visus.demos.kotlinapi.api.dto
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema
|
||||
import io.visus.demos.kotlinapi.domain.model.ComponentHealth
|
||||
import io.visus.demos.kotlinapi.domain.model.HealthStatus
|
||||
|
||||
@Schema(description = "Health check response")
|
||||
data class HealthResponse(
|
||||
@@ -12,18 +10,4 @@ data class HealthResponse(
|
||||
val timestamp: String,
|
||||
@Schema(description = "Status of individual components")
|
||||
val components: Map<String, ComponentHealthDto>? = null,
|
||||
) {
|
||||
companion object {
|
||||
fun from(healthStatus: HealthStatus): HealthResponse =
|
||||
HealthResponse(
|
||||
status = healthStatus.status.name,
|
||||
timestamp = healthStatus.timestamp.toString(),
|
||||
components =
|
||||
if (healthStatus.components.isNotEmpty()) {
|
||||
healthStatus.components.mapValues { ComponentHealthDto.from(it.value) }
|
||||
} else {
|
||||
null
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.visus.demos.kotlinapi.api.mappers
|
||||
|
||||
import io.visus.demos.kotlinapi.api.dto.ComponentHealthDto
|
||||
import io.visus.demos.kotlinapi.domain.model.ComponentHealth
|
||||
import tech.mappie.api.ObjectMappie
|
||||
|
||||
object ComponentHealthDtoMapper : ObjectMappie<ComponentHealth, ComponentHealthDto>() {
|
||||
override fun map(from: ComponentHealth): ComponentHealthDto {
|
||||
return ComponentHealthDto(
|
||||
status = from.status.name,
|
||||
message = from.message,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.visus.demos.kotlinapi.api.mappers
|
||||
|
||||
import io.visus.demos.kotlinapi.api.dto.HealthResponse
|
||||
import io.visus.demos.kotlinapi.domain.model.HealthStatus
|
||||
import tech.mappie.api.ObjectMappie
|
||||
|
||||
object HealthResponseMapper : ObjectMappie<HealthStatus, HealthResponse>() {
|
||||
override fun map(from: HealthStatus): HealthResponse {
|
||||
return HealthResponse(
|
||||
status = from.status.name,
|
||||
timestamp = from.timestamp.toString(),
|
||||
components = from.components.mapValues { (_, componentHealth) ->
|
||||
ComponentHealthDtoMapper.map(componentHealth)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ 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.api.mappers.HealthResponseMapper
|
||||
import io.visus.demos.kotlinapi.domain.model.HealthStatus
|
||||
import io.visus.demos.kotlinapi.domain.model.Status
|
||||
import io.visus.demos.kotlinapi.domain.service.HealthService
|
||||
@@ -50,7 +51,7 @@ class HealthController(
|
||||
fun health(): ResponseEntity<HealthResponse> {
|
||||
val componentHealths = healthCheckServices.map { it.check() }
|
||||
val healthStatus = HealthStatus.fromComponents(componentHealths)
|
||||
val response = HealthResponse.from(healthStatus)
|
||||
val response = HealthResponseMapper.map(healthStatus)
|
||||
|
||||
val httpStatus =
|
||||
when (healthStatus.status) {
|
||||
|
||||
Reference in New Issue
Block a user