-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_handler.go
58 lines (51 loc) · 1.14 KB
/
debug_handler.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
package mhist
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
// DebugHandler exposes a debug port over http, used for pprof
type DebugHandler struct {
Port int
httpServer *http.Server
server *Server
}
// Run listens on the given port and serves http
func (h *DebugHandler) Run() {
h.httpServer = &http.Server{
Addr: fmt.Sprintf(":%v", h.Port),
}
http.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) {
infos := h.server.store.diskStore.GetAllStoredInfos()
b, err := json.Marshal(infos)
if err != nil {
log.Println(err)
w.WriteHeader(500)
return
}
_, err = w.Write(b)
if err != nil {
log.Println(err)
w.WriteHeader(500)
return
}
w.WriteHeader(200)
})
log.Println("debug_handler running on ", h.httpServer.Addr)
err := h.httpServer.ListenAndServe()
if err != nil {
log.Println(err)
}
}
// Shutdown the debug listener
func (h *DebugHandler) Shutdown() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := h.httpServer.Shutdown(ctx)
if err != nil {
log.Println("err while shutting debug handler down:", err)
}
}