ctucx.git: fritzbox-exporter

[nimlang] prometheus exporter for lte fritzboxes

commit fa1b11f15fa8beb1b577efffc7bc38177d518759
parent aa7cdd2d943193eff9aab302d2583e1c01779322
Author: ctucx <c@ctu.cx>
Date: Mon, 20 Apr 2020 04:35:40 +0200

foo
2 files changed, 19 insertions(+), 17 deletions(-)
M
src/asyncHttpServer.nim
|
18
+++++++-----------
M
src/fb_exporter.nim
|
18
++++++++++++------
diff --git a/src/asyncHttpServer.nim b/src/asyncHttpServer.nim
@@ -179,19 +179,15 @@ macro pages*(server: ServerRef, body: untyped): untyped =
           newNimNode(nnkElifBranch).add(
             newCall("match", ident("decoded_url"), path),
             slist))
-      elif current == "notfound":
-        notfound_declaration = true
-        ifstmtlist.add(newNimNode(nnkElse).add(slist))
-
-  if not notfound_declaration:
-    ifstmtlist.add(
-      newNimNode(nnkElse).add(
-        newCall(  # await request.respond(Http404, "Not found")
-          "await",
-          newCall("respond", ident("request"), ident("Http404"), newLit("Not found"))
-        )
+
+  ifstmtlist.add(
+    newNimNode(nnkElse).add(
+      newCall(  # await request.respond(Http404, "Not found")
+        "await",
+        newCall("respond", ident("request"), ident("Http404"), newLit("Not found"), newCall("newHttpHeaders"))
       )
     )
+  )
 
   result = newNimNode(nnkProcDef).add(
     ident("receivepages"),  # procedure name.
diff --git a/src/fb_exporter.nim b/src/fb_exporter.nim
@@ -74,7 +74,8 @@ proc prometheusResponse* (request: Request, state: JsonNode) {.async.} =
   var res = ""
 
   if state["inetstat"]["lastUpdated"].getInt == 0 and state["mobiled"]["lastUpdated"].getInt == 0:
-    await request.respond(Http500, "500 No data yet")
+    await request.respond(Http500, "500 No data yet", newHttpHeaders([("Content-Type","text/plain")]))
+
 
   if state["inetstat"]["lastUpdated"].getInt != 0:
     let data        = state["inetstat"]["data"]

@@ -135,7 +136,7 @@ proc prometheusResponse* (request: Request, state: JsonNode) {.async.} =
         data.parseCell(0) 
         data.parseCell(1)
 
-  await request.respond(Http200, res)
+  await request.respond(Http200, res, newHttpHeaders([("Content-Type","text/plain")]))
 
 proc main =  # for gcsafe
   setControlCHook(CtrlCHook)

@@ -158,6 +159,9 @@ proc main =  # for gcsafe
     }
 
   server.pages:
+    equals("/"):
+      await request.respond(Http200, "Hello, nothing to see here", newHttpHeaders([("Content-Type","text/plain")]))
+
     equals("/metrics"):
       await request.prometheusResponse(state)
 

@@ -179,17 +183,19 @@ proc main =  # for gcsafe
           if url == "inetstat":
             state["inetstat"]["data"]        =  parseCtlMgr(request.body)
             state["inetstat"]["lastUpdated"] = %toUnix(getTime())
-            await request.respond(Http200, "Noted, thanks")
+            await request.respond(Http200, "Noted, thanks", newHttpHeaders([("Content-Type","text/plain")]))
 
           elif url == "mobiled":
             state["mobiled"]["data"]         =  parseCtlMgr(request.body)
             state["mobiled"]["lastUpdated"]  = %toUnix(getTime())
-            await request.respond(Http200, "Noted, thanks")
+            await request.respond(Http200, "Noted, thanks", newHttpHeaders([("Content-Type","text/plain")]))
 
           else:
-            await request.respond(Http404, "404 Not found")
+            await request.respond(Http404, "404 Not found", newHttpHeaders([("Content-Type","text/plain")]))
         else:
-          await request.respond(Http401, "401 Unauthorized")
+          await request.respond(Http401, "401 Unauthorized", newHttpHeaders([("Content-Type","text/plain")]))
+      else:
+        await request.respond(Http404, "404 Not found", newHttpHeaders([("Content-Type","text/plain")]))        
 
   server.start()