github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/http/mws/pprof.go (about)

     1  package mws
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/pprof"
     6  	"strings"
     7  )
     8  
     9  func PProfHandler(enabled bool) func(handler http.Handler) http.Handler {
    10  	return func(handler http.Handler) http.Handler {
    11  		return &pprofHandler{
    12  			enabled: enabled,
    13  			next:    handler,
    14  		}
    15  	}
    16  }
    17  
    18  type pprofHandler struct {
    19  	enabled bool
    20  	next    http.Handler
    21  }
    22  
    23  func (h *pprofHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    24  	if h.enabled && strings.HasPrefix(req.URL.Path, "/debug/pprof") {
    25  		switch req.URL.Path {
    26  		case "/debug/pprof/cmdline":
    27  			pprof.Cmdline(rw, req)
    28  			return
    29  		case "/debug/pprof/profile":
    30  			pprof.Profile(rw, req)
    31  			return
    32  		case "/debug/pprof/symbol":
    33  			pprof.Symbol(rw, req)
    34  			return
    35  		case "/debug/pprof/trace":
    36  			pprof.Trace(rw, req)
    37  			return
    38  		default:
    39  			pprof.Index(rw, req)
    40  			return
    41  		}
    42  	}
    43  	h.next.ServeHTTP(rw, req)
    44  }