github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/api/server/router/debug/debug.go (about)

     1  package debug // import "github.com/docker/docker/api/server/router/debug"
     2  
     3  import (
     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.
    15  func NewRouter() router.Router {
    16  	r := &debugRouter{}
    17  	r.initRoutes()
    18  	return r
    19  }
    20  
    21  type debugRouter struct {
    22  	routes []router.Route
    23  }
    24  
    25  func (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  
    37  func (r *debugRouter) Routes() []router.Route {
    38  	return r.routes
    39  }
    40  
    41  func 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  
    48  func 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  }