/
diag.go
51 lines (45 loc) · 1.3 KB
/
diag.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
package diag
import (
"net"
"net/http"
"net/http/pprof"
"time"
"github.com/prasannavl/go-gluons/http/httpservice"
"github.com/prasannavl/go-gluons/log"
)
func SetupPprof(mux *http.ServeMux) {
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
}
func Create(addr string) httpservice.Service {
return CreateWithConfigure(addr, nil)
}
func CreateWithConfigure(addr string, configFn ...func(*http.ServeMux)) httpservice.Service {
l, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
log.Infof("diag endpoint: %s", l.Addr())
mux := http.NewServeMux()
SetupPprof(mux)
for _, x := range configFn {
x(mux)
}
server := &http.Server{
Handler: mux,
IdleTimeout: 20 * time.Second,
ReadTimeout: 20 * time.Second,
WriteTimeout: 60 * time.Second,
ErrorLog: nil,
MaxHeaderBytes: 1 << 12, // 4kb
}
return httpservice.New("diag", server, l)
}
func SetupIndexNotFound(mux *http.ServeMux) {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
}