summaryrefslogtreecommitdiff
path: root/internal/debug/server.go
blob: 6a75805512191072d9a334dee6e13aa2ce5023a2 (plain)
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
package debug

import (
	"context"
	"net/http"
	"net/http/pprof"
	"time"
)

// Server is a HTTP server for debugging.
type Server struct {
	srv *http.Server
}

// NewServer creates new HTTP server for debugging.
func NewServer() Server {
	mux := http.NewServeMux()

	mux.HandleFunc("/debug/pprof/", pprof.Index)
	mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
	mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
	mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
	mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

	return Server{srv: &http.Server{
		ReadHeaderTimeout: time.Minute * 10,
		Handler:           mux,
	}}
}

// Start debug server.
func (s *Server) Start(addr string) error {
	s.srv.Addr = addr

	return s.srv.ListenAndServe()
}

// Stop debug server.
func (s *Server) Stop(ctx context.Context) error {
	return s.srv.Shutdown(ctx)
}