From bf2ab576fe3aa6bdf90a6d0cec775e8a79263148 Mon Sep 17 00:00:00 2001
From: gjahn <gregor.jahn@bht-berlin.de>
Date: Wed, 1 Nov 2023 00:13:00 +0100
Subject: [PATCH] Fix health endpoint

it appears that order matters; using ctx.JSON overrides c.Type; but
instead of shuffling the lines around, the JSON string is generated
by *foot*, which allows to keep header setting at the beginning of
the function.
---
 routing/routes.go      | 20 ++++++++++++--------
 routing/routes_test.go |  4 ++--
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/routing/routes.go b/routing/routes.go
index 19f38d3..b80eda1 100644
--- a/routing/routes.go
+++ b/routing/routes.go
@@ -21,25 +21,29 @@ func SetRoutes( router *f.App, config *configuration.Config, healthiness *bool )
 
     router.Get( "/health", func( c *f.Ctx ) error {
         type response struct {
-            Status  string  `json:"status"  validate:"oneof=passed failed"`
+            Status  string  `json:"status"  validate:"oneof=pass fail"`
         }
 
-        c.Type( "json", "utf-8" )
+        c.Set( "Content-Type", "application/health+json; charset=utf-8" )
 
-        var res response
+        var res *response
         if *healthiness == false {
-            res = response{
-                Status: "failed",
+            res = &response{
+                Status: "fail",
             }
             c.Status( http.StatusServiceUnavailable )
         } else {
-            res = response{
-                Status: "passed",
+            res = &response{
+                Status: "pass",
             }
             c.Status( http.StatusOK )
         }
 
-        return c.JSON( res )
+        resJson, err := json.Marshal( res )
+        if err != nil {
+            return err
+        }
+        return c.SendString( string( resJson ) )
     })
 
 
diff --git a/routing/routes_test.go b/routing/routes_test.go
index 09bb4d1..f37924f 100644
--- a/routing/routes_test.go
+++ b/routing/routes_test.go
@@ -88,7 +88,7 @@ func TestHealthRoute( t *testing.T ){
     status := bodyContent[ "status" ].( string )
     assert.Equal( t, http.StatusOK, res.StatusCode )
     assert.Nil( t, err )
-    assert.Equal( t, "passed", status )
+    assert.Equal( t, "pass", status )
 
     *healthiness = false
 
@@ -98,7 +98,7 @@ func TestHealthRoute( t *testing.T ){
     status = bodyContent[ "status" ].( string )
     assert.Equal( t, http.StatusServiceUnavailable, res.StatusCode )
     assert.Nil( t, err )
-    assert.Equal( t, "failed", status )
+    assert.Equal( t, "fail", status )
 }
 
 
-- 
GitLab