github.com/cookieai-jar/moby@v17.12.1-ce-rc2+incompatible/api/server/router/debug/debug.go (about)

     1  package debug
     2  
     3  import (
     4  	"expvar"
     5  	"net/http"
     6  	"net/http/pprof"
     7  
     8  	"github.com/docker/docker/api/server/httputils"
     9  	"github.com/docker/docker/api/server/router"
    10  	"golang.org/x/net/context"
    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  }