-
Notifications
You must be signed in to change notification settings - Fork 0
/
cannula.go
91 lines (72 loc) · 1.98 KB
/
cannula.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cannula
import (
"fmt"
"net"
"net/http"
"os"
"sort"
"sync"
"github.com/retailnext/cannula/expvar"
"github.com/retailnext/cannula/internal/net/http/pprof"
)
var (
defaultServer = newServer()
)
// Start listens on the unix socket specified by path and then calls Serve.
func Start(path string) error {
// cleanup old instances of our domain socket
os.Remove(path)
ln, err := net.Listen("unix", path)
if err != nil {
return err
}
return Serve(ln)
}
// Serve starts serving debug http requests on listener.
func Serve(listener net.Listener) error {
httpServer := http.Server{Handler: defaultServer.mux}
return httpServer.Serve(listener)
}
// HandleFunc registers a new debug http.HandlerFunc.
func HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
Handle(pattern, http.HandlerFunc(handler))
}
// Handle registers a new debug handler.
func Handle(pattern string, handler http.Handler) {
defaultServer.Handle(pattern, handler)
}
type server struct {
sync.Mutex
mux *http.ServeMux
paths []string
}
func newServer() *server {
s := &server{
mux: http.NewServeMux(),
}
s.mux.HandleFunc("/", s.index)
// install net/http/pprof handlers also
s.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
s.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
s.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
s.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
s.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
s.Handle("/debug/vars", http.HandlerFunc(expvar.Handler))
return s
}
func (s *server) index(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, "Available handlers:")
s.Lock()
defer s.Unlock()
sort.Strings(s.paths)
for _, path := range s.paths {
fmt.Fprintln(w, path)
}
}
func (s *server) Handle(pattern string, handler http.Handler) {
s.mux.Handle(pattern, handler)
s.Lock()
defer s.Unlock()
s.paths = append(s.paths, pattern)
}