支持注册pprof相关路由 #6
1
go.mod
1
go.mod
@ -33,6 +33,7 @@ require (
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/pprof v1.5.2 // indirect
|
||||
github.com/gin-contrib/sse v1.0.0 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-openapi/jsonpointer v0.21.0 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -51,6 +51,8 @@ github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3G
|
||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
|
||||
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
|
||||
github.com/gin-contrib/pprof v1.5.2 h1:Kcq5W2bA2PBcVtF0MqkQjpvCpwJr+pd7zxcQh2csg7E=
|
||||
github.com/gin-contrib/pprof v1.5.2/go.mod h1:a1W4CDXwAPm2zql2AKdnT7OVCJdV/oFPhJXVOrDs5Ns=
|
||||
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
|
||||
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
|
@ -27,6 +27,7 @@ type serverOption struct {
|
||||
disableSwaggerDoc bool // 禁用swagger文档, 特定环境不想展示文档, 可通过次方式禁用
|
||||
serverInfo *apiDocDefine.Info // 服务器信息
|
||||
serverList []*apiDocDefine.ServerItem // 服务器环境列表
|
||||
enablePprof bool // 启用pprof
|
||||
}
|
||||
|
||||
// WithSwaggerUITheme 设置swaggerUI主题
|
||||
@ -109,3 +110,14 @@ func WithServerList(serverList []*apiDocDefine.ServerItem) SetServerOptionFunc {
|
||||
so.serverList = serverList
|
||||
}
|
||||
}
|
||||
|
||||
// WithPprofEnable 启用pprof
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:10 2025/2/21
|
||||
func WithPprofEnable() SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
so.enablePprof = true
|
||||
}
|
||||
}
|
||||
|
@ -35,12 +35,12 @@ func init() {
|
||||
//
|
||||
// Date : 21:40 2024/7/20
|
||||
func Register(port int, controllerList ...any) error {
|
||||
for _, controller := range controllerList {
|
||||
if nil == controller {
|
||||
for _, itemController := range controllerList {
|
||||
if nil == itemController {
|
||||
// 忽略空指针
|
||||
continue
|
||||
}
|
||||
parseController(controller)
|
||||
parseController(itemController)
|
||||
}
|
||||
return ginRouter.Run(fmt.Sprintf(":%d", port))
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
apiDoc "git.zhangdeman.cn/gateway/api-doc"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/middleware"
|
||||
"github.com/gin-contrib/pprof"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@ -80,6 +81,10 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
// 启用全局中间件
|
||||
r.Use(option.globalMiddlewareList...)
|
||||
}
|
||||
// 启用pprof, 注册相关路由
|
||||
if option.enablePprof {
|
||||
pprof.Register(r)
|
||||
}
|
||||
return &server{
|
||||
router: r,
|
||||
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, option.swaggerUiTheme),
|
||||
|
Loading…
x
Reference in New Issue
Block a user