nwaku/waku/node/rest/health/handlers.nim
NagyZoltanPeter fc6194bb6b
feat: Rest endoint /health for rln (#2011)
* Rest endoint /health added

* Remove dev-debug echo

* Changed not ready message

* Update waku/node/rest/health/handlers.nim

Co-authored-by: Ivan Folgueira Bande <128452529+Ivansete-status@users.noreply.github.com>

* Various fixes on PR foundings, added comments

---------

Co-authored-by: Ivan Folgueira Bande <128452529+Ivansete-status@users.noreply.github.com>
2023-09-08 11:19:47 +02:00

43 lines
1.2 KiB
Nim

when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import
chronicles,
json_serialization,
presto/route
import
../../waku_node,
../responses,
../serdes
logScope:
topics = "waku node rest health_api"
const ROUTE_HEALTH* = "/health"
const FutIsReadyTimout = 5.seconds
proc installHealthApiHandler*(router: var RestRouter, node: WakuNode) =
## /health endpoint provides information about node readiness to caller.
## Currently it is restricted to checking RLN (if mounted) proper setup
## TODO: Leter to extend it to a broader information about each subsystem state
## report. Rest response to change to JSON structure that can hold exact detailed
## information.
router.api(MethodGet, ROUTE_HEALTH) do () -> RestApiResponse:
let isReadyStateFut = node.isReady()
if not await isReadyStateFut.withTimeout(FutIsReadyTimout):
return RestApiResponse.internalServerError("Health check timed out")
var msg = "Node is healthy"
var status = Http200
if not isReadyStateFut.read():
msg = "Node is not ready"
status = Http503
return RestApiResponse.textResponse(msg, status)