-// HealthCheckPingHandler processes "GET /_health/ping" requests
-func HealthCheckPingHandler(resp http.ResponseWriter, req *http.Request) {
- fn := func() interface{} {
- return map[string]string{"health": "OK"}
- }
-
- healthCheckDo(resp, req, fn)
-}
-
-// Any health check handlers can pass this "func" which returns json to healthCheckDo
-type healthCheckFunc func() interface{}
-
-func healthCheckDo(resp http.ResponseWriter, req *http.Request, fn healthCheckFunc) {
- msg, code := healthCheckAuth(resp, req)
- if msg != "" {
- http.Error(resp, msg, code)
- return
- }
-
- ok, err := json.Marshal(fn())
- if err != nil {
- http.Error(resp, err.Error(), 500)
- return
- }
-
- resp.Write(ok)
-}
-
-func healthCheckAuth(resp http.ResponseWriter, req *http.Request) (string, int) {
- if theConfig.ManagementToken == "" {
- return "disabled", http.StatusNotFound
- } else if h := req.Header.Get("Authorization"); h == "" {
- return "authorization required", http.StatusUnauthorized
- } else if h != "Bearer "+theConfig.ManagementToken {
- return "authorization error", http.StatusForbidden
- }
- return "", 0
-}
-