1package debug // import "github.com/docker/docker/api/server/router/debug"
2
3import (
4	"context"
5	"expvar"
6	"net/http"
7	"net/http/pprof"
8
9	"github.com/docker/docker/api/server/httputils"
10	"github.com/docker/docker/api/server/router"
11)
12
13// NewRouter creates a new debug router
14// The debug router holds endpoints for debug the daemon, such as those for pprof.
15func NewRouter() router.Router {
16	r := &debugRouter{}
17	r.initRoutes()
18	return r
19}
20
21type debugRouter struct {
22	routes []router.Route
23}
24
25func (r *debugRouter) initRoutes() {
26	r.routes = []router.Route{
27		router.NewGetRoute("/vars", frameworkAdaptHandler(expvar.Handler())),
28		router.NewGetRoute("/pprof/", frameworkAdaptHandlerFunc(pprof.Index)),
29		router.NewGetRoute("/pprof/cmdline", frameworkAdaptHandlerFunc(pprof.Cmdline)),
30		router.NewGetRoute("/pprof/profile", frameworkAdaptHandlerFunc(pprof.Profile)),
31		router.NewGetRoute("/pprof/symbol", frameworkAdaptHandlerFunc(pprof.Symbol)),
32		router.NewGetRoute("/pprof/trace", frameworkAdaptHandlerFunc(pprof.Trace)),
33		router.NewGetRoute("/pprof/{name}", handlePprof),
34	}
35}
36
37func (r *debugRouter) Routes() []router.Route {
38	return r.routes
39}
40
41func frameworkAdaptHandler(handler http.Handler) httputils.APIFunc {
42	return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
43		handler.ServeHTTP(w, r)
44		return nil
45	}
46}
47
48func frameworkAdaptHandlerFunc(handler http.HandlerFunc) httputils.APIFunc {
49	return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
50		handler(w, r)
51		return nil
52	}
53}
54