-
Notifications
You must be signed in to change notification settings - Fork 22
/
router_api.go
74 lines (63 loc) · 1.76 KB
/
router_api.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package router
import (
"encoding/json"
"net/http"
"runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func NewAPIHandler(rout *Router) (api http.Handler, err error) {
mux := http.NewServeMux()
mux.HandleFunc("/reload", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Send a message to the Router goroutine which will check the latest
// oplog optime and start a reload if necessary.
// If the channel is already full, no message will be sent and the request
// won't be blocked.
select {
case rout.ReloadChan <- true:
default:
}
logInfo("router: reload queued")
w.WriteHeader(http.StatusAccepted)
_, err := w.Write([]byte("Reload queued"))
if err != nil {
logWarn(err)
}
})
mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
_, err := w.Write([]byte("OK"))
if err != nil {
logWarn(err)
}
})
mux.HandleFunc("/memory-stats", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
memStats := &runtime.MemStats{}
runtime.ReadMemStats(memStats)
jsonData, err := json.MarshalIndent(memStats, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(jsonData)
if err != nil {
logWarn(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
mux.Handle("/metrics", promhttp.Handler())
return mux, nil
}