diff --git a/server.go b/server.go index 1a3fd28..a800b18 100644 --- a/server.go +++ b/server.go @@ -1,6 +1,7 @@ //3456789112345676892123456789312345678941234567895123456789612345678971234567898 package main +import "encoding/json" import "fmt" import "net/http" import "os" @@ -15,6 +16,23 @@ func serve() { s.ListenAndServe() } +func getHealthCheckHandler() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + result := gin.H{ + "status": "ok", + "now": time.Now().UTC().Format(time.RFC3339), + } + json, err := json.Marshal(result) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.Write(json) + } +} + func getRouter() *gin.Engine { if os.Getenv("DEBUG") == "" { @@ -30,12 +48,8 @@ func getRouter() *gin.Engine { // attach logger middleware r.Use(ginzerolog.Logger("gin")) - r.GET("/.well-known/healthcheck.json", func(c *gin.Context) { - c.JSON(200, gin.H{ - "status": "ok", - "now": time.Now().UTC().Format(time.RFC3339), - }) - }) + r.GET("/.well-known/healthcheck.json", gin.WrapF(getHealthCheckHandler())) + r.GET("/admin/healthcheck.json", gin.WrapF(getHealthCheckHandler())) // call it, it returns the appropriate handler function // so we can execute some code at startup time