-
Notifications
You must be signed in to change notification settings - Fork 2
/
healthcheck.go
36 lines (30 loc) · 926 Bytes
/
healthcheck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"net/http"
"strconv"
"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
var (
router = mux.NewRouter()
)
func (c *Context) startHealthcheck() {
router.HandleFunc("/", c.healtcheck).Methods("GET", "HEAD").Name("Healthcheck")
logrus.Info("Healthcheck handler is listening on ", healtcheckPort)
logrus.Fatal(http.ListenAndServe(":"+strconv.Itoa(healtcheckPort), router))
}
func (c *Context) healtcheck(w http.ResponseWriter, req *http.Request) {
_, err := c.Rancher.Client.GetSelfStack()
if err != nil {
logrus.Error("Healtcheck failed: unable to reach metadata")
http.Error(w, "Failed to reach metadata server", http.StatusInternalServerError)
} else {
_, err := c.Consul.Ping()
if err != nil {
logrus.Errorf("Failed to reach Consul API: %v", err)
http.Error(w, "Failed to reach Consul API ", http.StatusInternalServerError)
} else {
w.Write([]byte("OK"))
}
}
}