Implement health checking for HTTP(S)
Anirudh Oppiliappan x@icyphox.sh
Fri, 24 Jul 2020 10:56:10 +0530
1 files changed,
22 insertions(+),
7 deletions(-)
jump to
M
src/wsabi.nim
→
src/wsabi.nim
@@ -5,6 +5,7 @@ asynchttpserver,
strformat, asyncnet, wsabipkg/args, + httpclient, strutils, json, uri@@ -67,13 +68,6 @@ let
hcJson = parseJson(req.body) hc = to(hcJson, HealthCheck) - if hc.uri notin ["ws", "wss", "http", "https"]: - let r = %*{"status": "error", "msg": &"unknown URI {hc.uri}"} - await req.respond( - Http400, $r, - newHttpHeaders([("Content-Type", "application/json")]) - ) - ## Check if host is a websocket, and try for the xmpp protocol if hc.uri in ["ws", "wss"]: let hostStr = &"{hc.uri}://{hc.host}:{hc.port}/ws/"@@ -86,10 +80,31 @@
echo await ws.receiveStrPacket() ws.close() let r = %*{"status": "OK"} + await req.respond( Http200, $r, newHttpHeaders([("Content-Type", "application/json")]) ) + + if hc.uri in ["http", "https"]: + let hostStr = &"{hc.uri}://{hc.host}:{hc.port}/" + echo &"connecting to {hostStr}" + var tmpClient = newAsyncHttpClient() + echo await tmpClient.getContent(hostStr) + let r = %*{"status": "OK"} + + await req.respond( + Http200, $r, + newHttpHeaders([("Content-Type", "application/json")]) + ) + + else: + let r = %*{"status": "error", "msg": &"unknown URI {hc.uri}"} + await req.respond( + Http400, $r, + newHttpHeaders([("Content-Type", "application/json")]) + ) + except OSError as e: echo "unable to reach specified host:port pair" let r = %*{