-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.go
More file actions
51 lines (44 loc) · 1.33 KB
/
debug.go
File metadata and controls
51 lines (44 loc) · 1.33 KB
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
package gee
import (
"net/http"
"net/http/pprof"
"path/filepath"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var debugToken = "058759a8c4d920576348854616a58f3f"
func SetDebugToken(token string) {
debugToken = token
}
var authDebug = func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("token") != debugToken {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
func DebugRoute(r Router, path ...string) {
p := "/debug"
// custom router path
if len(path) > 0 && path[0] != "" {
p = filepath.Join(path[0], p)
}
r.Use(authDebug)
// pporf
g := r.Group(p)
g.Get("/", http.HandlerFunc(pprof.Index))
g.Get("/cmdline", http.HandlerFunc(pprof.Cmdline))
g.Get("/profile", http.HandlerFunc(pprof.Profile))
g.Post("/symbol", http.HandlerFunc(pprof.Symbol))
g.Get("/symbol", http.HandlerFunc(pprof.Symbol))
g.Get("/trace", http.HandlerFunc(pprof.Trace))
g.Handle("/allocs", pprof.Handler("allocs"))
g.Handle("/block", pprof.Handler("block"))
g.Handle("/goroutine", pprof.Handler("goroutine"))
g.Handle("/heap", pprof.Handler("heap"))
g.Handle("/mutex", pprof.Handler("mutex"))
g.Handle("/threadcreate", pprof.Handler("threadcreate"))
// prometheus
g.Handle("/metrics", promhttp.Handler())
}