Skip to content

Commit 7fd5039

Browse files
committed
docs: add custom router group
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 4b18fd3 commit 7fd5039

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@ func main() {
5454
}
5555
```
5656

57+
### custom router group
58+
59+
```go
60+
package main
61+
62+
import (
63+
"net/http"
64+
65+
"github.com/gin-contrib/pprof"
66+
"github.com/gin-gonic/gin"
67+
)
68+
69+
func main() {
70+
router := gin.Default()
71+
pprof.Register(router)
72+
adminGroup := r.Group("/admin", func(c *gin.Context) {
73+
if c.Request.Header.Get("Authorization") != "foobar" {
74+
c.AbortWithStatus(http.StatusForbidden)
75+
return
76+
}
77+
c.Next()
78+
})
79+
pprof.RouteRegister(adminGroup, "pprof")
80+
router.Run(":8080")
81+
}
82+
83+
```
84+
5785
### Use the pprof tool
5886

5987
Then use the pprof tool to look at the heap profile:

example/custom/server.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-contrib/pprof"
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
func main() {
11+
router := gin.Default()
12+
pprof.Register(router)
13+
adminGroup := r.Group("/admin", func(c *gin.Context) {
14+
if c.Request.Header.Get("Authorization") != "foobar" {
15+
c.AbortWithStatus(http.StatusForbidden)
16+
return
17+
}
18+
c.Next()
19+
})
20+
pprof.RouteRegister(adminGroup, "pprof")
21+
router.Run(":8080")
22+
}

0 commit comments

Comments
 (0)