func initRoutes() bool { if inited { return false } inited = true Handle("/goroutine", "Get goroutine info", pprofLookupHandler("goroutine")) Handle("/heap", "Get heap info", pprofLookupHandler("heap")) Handle("/thread", "Get thread create info", pprofLookupHandler("threadcreate")) Handle("/block", "Get block info", pprofLookupHandler("block")) Handle("/cpu", "Get CPU info, default seconds is 30, use ?seconds= to reset", func(req zerver.Request, resp zerver.Response) { var t int if secs := req.Vars().QueryVar("seconds"); secs != "" { var err error if t, err = strconv.Atoi(secs); err != nil { resp.StatusCode(http.StatusBadRequest) io2.WriteString(resp, secs+" is not a integer number\n") return } } if t <= 0 { t = 30 } pprof.StartCPUProfile(resp) time.Sleep(time.Duration(t) * time.Second) pprof.StopCPUProfile() }) Handle("/memory", "Get memory info", func(req zerver.Request, resp zerver.Response) { runtime.GC() pprof.WriteHeapProfile(resp) }) Handle("/routes", "Get all routes", func(req zerver.Request, resp zerver.Response) { req.Server().PrintRouteTree(resp) }) Handle("/options", "Get all pprof options", func(req zerver.Request, resp zerver.Response) { if from := req.Vars().QueryVar("from"); from != "" { resp.Write(unsafe2.Bytes("There is no this pprof option: " + from + "\n")) } for i := range options { resp.Write(unsafe2.Bytes(options[i])) } }) return inited }
func globalFilter(req zerver.Request, resp zerver.Response, chain zerver.FilterChain) { status := resp.StatusCode(0) if status == http.StatusNotFound { resp.Headers().Set("Location", path+"/options?from="+url.QueryEscape(req.URL().Path)) resp.StatusCode(http.StatusMovedPermanently) } else if status == http.StatusMethodNotAllowed { io2.WriteString(resp, "The pprof interface only support GET request\n") } else { chain(req, resp) } }
func (j *JSONP) Filter(req zerver.Request, resp zerver.Response, chain zerver.FilterChain) { if req.ReqMethod() != zerver.METHOD_GET { chain(req, resp) return } callback := req.Vars().QueryVar(j.CallbackVar) if callback == "" { chain(req, resp) return } buffer := bytes.NewBuffer(make([]byte, 0, 256)) bw := wrap.BuffRespWriter{ // to avoid write header 200 first when write callback name Buffer: buffer, } resp.Wrap(func(w http.ResponseWriter, shouldClose bool) (http.ResponseWriter, bool) { bw.ResponseWriter = w bw.ShouldClose = shouldClose return &bw, shouldClose }) chain(req, resp) bw.Buffer = nil _, err := io2.WriteString(resp, callback) if err == nil { _, err = io2.WriteString(resp, "(") if err == nil { _, err = resp.Write(buffer.Bytes()) if err == nil { _, err = io2.WriteString(resp, ")") } } } if err != nil { j.log.Warn(log.M{"msg": "write jsonp response failed", "err": err.Error()}) } }