From 7c575585e9e305a28cc138d778f3f0bef5a6909a Mon Sep 17 00:00:00 2001 From: Eyitayo Ogunbiyi Date: Mon, 6 Jul 2020 15:27:12 +0100 Subject: [PATCH] added conditional json flag when trying to view healthchecks --- admin.go | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/admin.go b/admin.go index 3e538a0e..c5ae686d 100644 --- a/admin.go +++ b/admin.go @@ -279,9 +279,7 @@ func profIndex(rw http.ResponseWriter, r *http.Request) { http.Error(rw, err.Error(), http.StatusInternalServerError) return } - - rw.Header().Set("Content-Type", "application/json") - rw.Write(dataJSON) + execJSON(rw, dataJSON) return } @@ -295,7 +293,7 @@ func profIndex(rw http.ResponseWriter, r *http.Request) { // Healthcheck is a http.Handler calling health checking and showing the result. // it's in "/healthcheck" pattern in admin module. -func healthcheck(rw http.ResponseWriter, _ *http.Request) { +func healthcheck(rw http.ResponseWriter, r *http.Request) { var ( result []string data = make(map[interface{}]interface{}) @@ -322,12 +320,44 @@ func healthcheck(rw http.ResponseWriter, _ *http.Request) { *resultList = append(*resultList, result) } + queryParams := r.URL.Query() + + if queryParams["json"] != nil { + + type Result map[string]interface{} + + response := make([]Result, len(*resultList)) + + for i, currentResult := range *resultList { + currentResultMap := make(Result) + currentResultMap["name"] = currentResult[0] + currentResultMap["message"] = currentResult[1] + currentResultMap["status"] = currentResult[2] + response[i] = currentResultMap + } + + JSONResponse, err := json.Marshal(response) + if err != nil { + http.Error(rw, err.Error(), http.StatusInternalServerError) + } else { + execJSON(rw, JSONResponse) + } + + return + } + content["Data"] = resultList data["Content"] = content data["Title"] = "Health Check" + execTpl(rw, data, healthCheckTpl, defaultScriptsTpl) } +func execJSON(rw http.ResponseWriter, jsonData []byte) { + rw.Header().Set("Content-Type", "application/json") + rw.Write(jsonData) +} + // TaskStatus is a http.Handler with running task status (task name, status and the last execution). // it's in "/task" pattern in admin module. func taskStatus(rw http.ResponseWriter, req *http.Request) {