0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/pprof/pprof.go
2016-03-18 10:39:29 -06:00

34 lines
1,006 B
Go

package pprof
import (
"net/http"
pp "net/http/pprof"
"github.com/mholt/caddy/middleware"
)
//Handler is a simple struct whose ServeHTTP will delegate relevant pprof endpoints to net/http/pprof
type handler struct {
mux *http.ServeMux
}
//New creates a new pprof middleware
func New(next middleware.Handler) middleware.Handler {
//pretty much copying what pprof does on init: https://golang.org/src/net/http/pprof/pprof.go#L67
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pp.Index)
mux.HandleFunc("/debug/pprof/cmdline", pp.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pp.Profile)
mux.HandleFunc("/debug/pprof/symbol", pp.Symbol)
mux.HandleFunc("/debug/pprof/trace", pp.Trace)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
return &handler{mux}
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
rec := middleware.NewResponseRecorder(w)
h.mux.ServeHTTP(rec, r)
return rec.Status(), nil
}