Health returns the status of the cluster (red, yellow, green).
(w http.ResponseWriter, r *http.Request)
| 58 | |
| 59 | // Health returns the status of the cluster (red, yellow, green). |
| 60 | func Health(w http.ResponseWriter, r *http.Request) { |
| 61 | var j map[string]interface{} |
| 62 | |
| 63 | res, err := ES.Cluster.Health() |
| 64 | if err != nil { |
| 65 | log.Printf("Error getting response from Elasticsearch: %s", err) |
| 66 | http.Error(w, `{"status" : "error"}`, http.StatusBadGateway) |
| 67 | return |
| 68 | } |
| 69 | defer res.Body.Close() |
| 70 | |
| 71 | if res.IsError() { |
| 72 | log.Printf("HTTP response error: %s", res.Status()) |
| 73 | http.Error(w, `{"status" : "error"}`, http.StatusBadGateway) |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | if err := json.NewDecoder(res.Body).Decode(&j); err != nil { |
| 78 | log.Printf("Error parsing the HTTP response body: %s", err) |
| 79 | http.Error(w, `{"status" : "error"}`, http.StatusInternalServerError) |
| 80 | return |
| 81 | } else { |
| 82 | w.Header().Set("Content-Type", "application/json") |
| 83 | w.WriteHeader(http.StatusOK) |
| 84 | fmt.Fprintf(w, `{"status":%q}`, j["status"].(string)) |
| 85 | } |
| 86 | } |