-
Notifications
You must be signed in to change notification settings - Fork 5
/
endpoints.go
45 lines (40 loc) · 1.15 KB
/
endpoints.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
package httputil
import (
"io"
"net/http"
"net/http/pprof"
"strings"
)
var healthInfo string
func SetHealthHandlerInfo(info string) {
healthInfo = info
}
func ServeHealth() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, "alive!")
if healthInfo != "" {
_, _ = io.WriteString(w, " "+healthInfo)
}
})
}
func ServePPROF(pprofPrefix string) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
endpoint := strings.TrimPrefix(request.URL.Path, pprofPrefix)
switch endpoint {
case "":
http.HandlerFunc(pprof.Index).ServeHTTP(writer, request)
case "cmdline":
http.HandlerFunc(pprof.Cmdline).ServeHTTP(writer, request)
case "profile":
http.HandlerFunc(pprof.Profile).ServeHTTP(writer, request)
case "symbol":
http.HandlerFunc(pprof.Symbol).ServeHTTP(writer, request)
case "trace":
http.HandlerFunc(pprof.Trace).ServeHTTP(writer, request)
case "allocs", "block", "goroutine", "heap", "mutex", "threadcreate":
pprof.Handler(endpoint).ServeHTTP(writer, request)
default:
writer.WriteHeader(http.StatusNotFound)
}
})
}