-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
healthcheck.go
47 lines (40 loc) · 1.02 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
package metrics
import (
"github.com/gorilla/mux"
"log"
"net/http"
)
var GlobalReady = false
func StartHealthCheckServer(port string) {
r := mux.NewRouter()
r.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("hell yeah"))
})
r.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
if GlobalReady {
resp, err := http.Get("https://discordapp.com/api/v8/gateway")
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer resp.Body.Close()
w.WriteHeader(resp.StatusCode)
if resp.StatusCode == http.StatusOK {
w.Write([]byte("ready"))
} else {
w.Write([]byte("unready"))
}
if resp.StatusCode == http.StatusTooManyRequests {
w.WriteHeader(http.StatusTooManyRequests)
log.Println("I'M BEING RATE-LIMITED BY DISCORD")
return
}
} else {
w.WriteHeader(http.StatusTooEarly)
}
})
http.ListenAndServe(":"+port, r)
}