From 7fd50398dc82952a9cfd7cc548adfbecc1d27f01 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 23 Apr 2020 10:44:13 +0800 Subject: [PATCH] docs: add custom router group Signed-off-by: Bo-Yi Wu --- README.md | 28 ++++++++++++++++++++++++++++ example/custom/server.go | 22 ++++++++++++++++++++++ example/{ => default}/server.go | 0 3 files changed, 50 insertions(+) create mode 100644 example/custom/server.go rename example/{ => default}/server.go (100%) diff --git a/README.md b/README.md index 4976e76..6676b76 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,34 @@ func main() { } ``` +### custom router group + +```go +package main + +import ( + "net/http" + + "github.com/gin-contrib/pprof" + "github.com/gin-gonic/gin" +) + +func main() { + router := gin.Default() + pprof.Register(router) + adminGroup := r.Group("/admin", func(c *gin.Context) { + if c.Request.Header.Get("Authorization") != "foobar" { + c.AbortWithStatus(http.StatusForbidden) + return + } + c.Next() + }) + pprof.RouteRegister(adminGroup, "pprof") + router.Run(":8080") +} + +``` + ### Use the pprof tool Then use the pprof tool to look at the heap profile: diff --git a/example/custom/server.go b/example/custom/server.go new file mode 100644 index 0000000..2b8b9a7 --- /dev/null +++ b/example/custom/server.go @@ -0,0 +1,22 @@ +package main + +import ( + "net/http" + + "github.com/gin-contrib/pprof" + "github.com/gin-gonic/gin" +) + +func main() { + router := gin.Default() + pprof.Register(router) + adminGroup := r.Group("/admin", func(c *gin.Context) { + if c.Request.Header.Get("Authorization") != "foobar" { + c.AbortWithStatus(http.StatusForbidden) + return + } + c.Next() + }) + pprof.RouteRegister(adminGroup, "pprof") + router.Run(":8080") +} diff --git a/example/server.go b/example/default/server.go similarity index 100% rename from example/server.go rename to example/default/server.go