Skip to content

Yet another recipe for using "/debug/*" handlers #251

Closed
@runningmaster

Description

@runningmaster

This is not a bug. This is yet another recipe for using "/debug/*" handlers. Also see https://github.com/echo-contrib/echopprof

package main

import (
    "net/http"

    _ "expvar"
    _ "net/http/pprof"

    "github.com/labstack/echo"
    "github.com/labstack/echo/middleware"
)

func main() {
    e := echo.New()

    // Middleware
    e.Use(
        middleware.Logger(),
        middleware.Recover(),
        middleware.Gzip(),
    )

    // Routes
    e.Get("/", hello)
    // ... some other handlers...

    // Group, Middleware and Routes for /debug/* from Go's stdlib
    // GET handlers (or POST if it needs)
    d := e.Group("/debug",
        middleware.Gzip(),
    )
    d.Get("/vars", wrapStdHandler)
    d.Get("/pprof/heap", wrapStdHandler)
    d.Get("/pprof/goroutine", wrapStdHandler)
    d.Get("/pprof/block", wrapStdHandler)
    d.Get("/pprof/threadcreate", wrapStdHandler)
    d.Get("/pprof/cmdline", wrapStdHandler)
    d.Get("/pprof/profile", wrapStdHandler)
    d.Get("/pprof/symbol", wrapStdHandler)
    d.Get("/pprof/trace", wrapStdHandler)

    // Start server
    e.Run(":1323")
}

// Some handler
func hello(c *echo.Context) error {
    return c.String(http.StatusOK, "Hello, World!\n")
}

// Wrapper for all stdlib /debug/* handlers
func wrapStdHandler(c *echo.Context) error {
    w, r := c.Response().Writer(), c.Request()
    if h, p := http.DefaultServeMux.Handler(r); len(p) != 0 {
        h.ServeHTTP(w, r)
        return nil
    }
    return echo.NewHTTPError(http.StatusNotFound)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions