Skip to content

Commit

Permalink
refactor: replace Spray-JSON with ZIO-JSON in health route (#2360)
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt committed Jan 3, 2023
1 parent b58828a commit 1b8e74b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
14 changes: 6 additions & 8 deletions webapi/src/main/scala/org/knora/webapi/routing/HealthRoute.scala
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
20 changes: 8 additions & 12 deletions webapi/src/main/scala/org/knora/webapi/routing/HealthRouteZ.scala
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

0 comments on commit 1b8e74b

Please sign in to comment.