Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace Spray-JSON with ZIO-JSON in health route #2360

Merged
Expand Up @@ -9,9 +9,8 @@ import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives.get
import akka.http.scaladsl.server.Directives.path
import akka.http.scaladsl.server.Route
import spray.json.JsObject
import spray.json.JsString
import zio._
import zio.json._

import org.knora.webapi.core.State
import org.knora.webapi.core.domain.AppState
Expand Down Expand Up @@ -64,12 +63,7 @@ trait HealthCheck {
status = statusCode(result.status),
entity = HttpEntity(
ContentTypes.`application/json`,
JsObject(
"name" -> JsString("AppState"),
"severity" -> JsString("non fatal"),
"status" -> JsString(status(result.status)),
"message" -> JsString(result.message)
).compactPrint
result.toJson
)
)
)
Expand All @@ -81,6 +75,10 @@ trait HealthCheck {

private case class HealthCheckResult(name: String, severity: String, status: Boolean, message: String)

private object HealthCheckResult {
implicit val encoder: JsonEncoder[HealthCheckResult] = DeriveJsonEncoder.gen[HealthCheckResult]
}

private def unhealthy(message: String) =
HealthCheckResult(
name = "AppState",
Expand Down
Expand Up @@ -5,10 +5,9 @@

package org.knora.webapi.instrumentation.health

import spray.json.JsObject
import spray.json.JsString
import zhttp.http._
import zio._
import zio.json._

import org.knora.webapi.core.State
import org.knora.webapi.core.domain.AppState
Expand Down Expand Up @@ -60,14 +59,7 @@ object HealthRouteZ {
private def createResponse(result: HealthCheckResult): UIO[Response] =
ZIO.succeed(
Response
.json(
JsObject(
"name" -> JsString("AppState"),
"severity" -> JsString("non fatal"),
"status" -> JsString(status(result.status)),
"message" -> JsString(result.message)
).toString()
)
.json(result.toJson)
.setStatus(statusCode(result.status))
)

Expand All @@ -90,13 +82,17 @@ object HealthRouteZ {
/**
* The result of a health check which is either unhealthy or healthy.
*
* @param name ???
* @param severity ???
* @param name always "AppState"
* @param severity severity of the health status. Always "non fatal", otherwise the application would not respond
* @param status the status (either false = unhealthy or true = healthy)
* @param message the message
*/
private case class HealthCheckResult(name: String, severity: String, status: Boolean, message: String)

private object HealthCheckResult {
implicit val encoder: JsonEncoder[HealthCheckResult] = DeriveJsonEncoder.gen[HealthCheckResult]
}

private def unhealthy(message: String) =
HealthCheckResult(
name = "AppState",
Expand Down