Skip to content

Commit 81289f8

Browse files
committed
Add a basic opinionated debug server
1 parent 99d2704 commit 81289f8

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

debug/server.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package debug
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/pprof"
7+
"sync"
8+
9+
"github.com/replicate/go/logging"
10+
)
11+
12+
var logger = logging.New("debug")
13+
14+
var defaultServeMux http.ServeMux
15+
16+
func init() {
17+
defaultServeMux.HandleFunc("/debug/pprof/", pprof.Index)
18+
defaultServeMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
19+
defaultServeMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
20+
defaultServeMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
21+
defaultServeMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
22+
}
23+
24+
type Server struct {
25+
Addr string
26+
27+
once sync.Once
28+
server *http.Server
29+
}
30+
31+
func (s *Server) init() {
32+
s.server = &http.Server{
33+
Addr: s.Addr,
34+
Handler: &defaultServeMux,
35+
}
36+
}
37+
38+
func (s *Server) ListenAndServe() error {
39+
s.once.Do(s.init)
40+
41+
logger.Sugar().Infow("starting debug http server", "address", s.Addr)
42+
return s.server.ListenAndServe()
43+
}
44+
45+
func (s *Server) Shutdown(ctx context.Context) error {
46+
s.once.Do(s.init)
47+
48+
return s.server.Shutdown(ctx)
49+
}

0 commit comments

Comments
 (0)