/
HealthRouteWithZIOHttp.scala
115 lines (103 loc) · 4.53 KB
/
HealthRouteWithZIOHttp.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.routing
import spray.json.JsObject
import spray.json.JsString
import zhttp.http._
import zio._
import org.knora.webapi.core.State
import org.knora.webapi.core.domain.AppState
/**
* Provides the '/healthZ' endpoint serving the health status.
*/
object HealthRouteWithZIOHttp {
def apply(): HttpApp[State, Nothing] =
Http.collectZIO[Request] { case Method.GET -> !! / "healthZ" =>
State.getAppState.map(toHealthCheckResult).flatMap(createResponse)
}
/**
* Transforms the [[AppState]] into a [[HealthCheckResult]]
*
* @param state the application's state
* @return the result which is either unhealthy or healthy, containing a human readable explanation in case of unhealthy
*/
private def toHealthCheckResult(state: AppState): HealthCheckResult =
state match {
case AppState.Stopped => unhealthy("Stopped. Please retry later.")
case AppState.StartingUp => unhealthy("Starting up. Please retry later.")
case AppState.WaitingForTriplestore => unhealthy("Waiting for triplestore. Please retry later.")
case AppState.TriplestoreReady => unhealthy("Triplestore ready. Please retry later.")
case AppState.UpdatingRepository => unhealthy("Updating repository. Please retry later.")
case AppState.RepositoryUpToDate => unhealthy("Repository up to date. Please retry later.")
case AppState.CreatingCaches => unhealthy("Creating caches. Please retry later.")
case AppState.CachesReady => unhealthy("Caches ready. Please retry later.")
case AppState.UpdatingSearchIndex => unhealthy("Updating search index. Please retry later.")
case AppState.SearchIndexReady => unhealthy("Search index ready. Please retry later.")
case AppState.LoadingOntologies => unhealthy("Loading ontologies. Please retry later.")
case AppState.OntologiesReady => unhealthy("Ontologies ready. Please retry later.")
case AppState.WaitingForIIIFService => unhealthy("Waiting for IIIF service. Please retry later.")
case AppState.IIIFServiceReady => unhealthy("IIIF service ready. Please retry later.")
case AppState.WaitingForCacheService => unhealthy("Waiting for cache service. Please retry later.")
case AppState.CacheServiceReady => unhealthy("Cache service ready. Please retry later.")
case AppState.MaintenanceMode => unhealthy("Application is in maintenance mode. Please retry later.")
case AppState.Running => healthy
}
/**
* creates the HTTP response from the health check result (healthy/unhealthy)
*
* @param result the result of the health check
* @return an HTTP response
*/
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()
)
.setStatus(statusCode(result.status))
)
/**
* returns a string representation "healthy" or "unhealthy" from a boolean
*
* @param s a boolean from which to derive the state
* @return either "healthy" or "unhealthy"
*/
private def status(s: Boolean): String = if (s) "healthy" else "unhealthy"
/**
* returns the HTTP status according to the input boolean
*
* @param s a boolean from which to derive the HTTP status
* @return the HTTP status (OK or ServiceUnavailable)
*/
private def statusCode(s: Boolean): Status = if (s) Status.Ok else Status.ServiceUnavailable
/**
* The result of a health check which is either unhealthy or healthy.
*
* @param name ???
* @param severity ???
* @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 def unhealthy(message: String) =
HealthCheckResult(
name = "AppState",
severity = "non fatal",
status = false,
message = message
)
private val healthy =
HealthCheckResult(
name = "AppState",
severity = "non fatal",
status = true,
message = "Application is healthy"
)
}