提交 469dc7be 编写于 作者: E Eyitayo Ogunbiyi

refactored the building of healthcheck response map

上级 ca0c64b6
......@@ -21,6 +21,7 @@ import (
"net/http"
"os"
"reflect"
"strconv"
"text/template"
"time"
......@@ -321,28 +322,18 @@ func healthcheck(rw http.ResponseWriter, r *http.Request) {
}
queryParams := r.URL.Query()
jsonFlag := queryParams.Get("json")
shouldReturnJSON, _ := strconv.ParseBool(jsonFlag)
if queryParams["json"] != nil {
if shouldReturnJSON {
responseMap := buildHealthCheckResponseMap(resultList)
jsonResponse, err := json.Marshal(responseMap)
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 {
writeJSON(rw, JSONResponse)
writeJSON(rw, jsonResponse)
}
return
}
......@@ -353,6 +344,23 @@ func healthcheck(rw http.ResponseWriter, r *http.Request) {
writeTemplate(rw, data, healthCheckTpl, defaultScriptsTpl)
}
func buildHealthCheckResponseMap(resultList *[][]string) []map[string]interface{} {
response := make([]map[string]interface{}, len(*resultList))
for i, currentResult := range *resultList {
currentResultMap := make(map[string]interface{})
currentResultMap["name"] = currentResult[0]
currentResultMap["message"] = currentResult[1]
currentResultMap["status"] = currentResult[2]
response[i] = currentResultMap
}
return response
}
func writeJSON(rw http.ResponseWriter, jsonData []byte) {
rw.Header().Set("Content-Type", "application/json")
rw.Write(jsonData)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册