支持注册pprof相关路由

This commit is contained in:
2025-02-21 15:16:43 +08:00
parent bc94242d12
commit 4909e75928
5 changed files with 23 additions and 3 deletions

View File

@ -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
}
}

View File

@ -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))
}

View File

@ -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),