feat: add pprof function

This commit is contained in:
engigu
2026-03-10 17:02:25 +08:00
parent 03ee39fccf
commit fdfb8fc06a
4 changed files with 25 additions and 4 deletions
+1
View File
@@ -50,6 +50,7 @@ require (
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.13 // indirect
github.com/gin-contrib/pprof v1.5.3 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
+2
View File
@@ -124,6 +124,8 @@ github.com/getkin/kin-openapi v0.133.0 h1:pJdmNohVIJ97r4AUFtEXRXwESr8b0bD721u/Tz
github.com/getkin/kin-openapi v0.133.0/go.mod h1:boAciF6cXk5FhPqe/NQeBTeenbjqU4LhWBf09ILVvWE=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/pprof v1.5.3 h1:Bj5SxJ3kQDVez/s/+f9+meedJIqLS+xlkIVDe/lcvgM=
github.com/gin-contrib/pprof v1.5.3/go.mod h1:0+LQSZ4SLO0B6+2n6JBzaEygpTBxe/nI+YEYpfQQ6xY=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
+6
View File
@@ -7,6 +7,7 @@ import (
"github.com/engigu/baihu-panel/internal/middleware"
"github.com/engigu/baihu-panel/internal/services"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
)
@@ -38,6 +39,11 @@ func Setup(c *Controllers) *gin.Engine {
cfg := services.GetConfig()
urlPrefix := strings.TrimSuffix(cfg.Server.URLPrefix, "/")
// 按需绑定 Pprof 调试路由
if cfg.Server.PprofEnabled {
pprof.Register(router)
}
// 创建一个路由组,如果有前缀则使用前缀,否则使用根路径
var root *gin.RouterGroup
if urlPrefix != "" {
+16 -4
View File
@@ -12,8 +12,9 @@ import (
type ServerConfig struct {
Port int `ini:"port"`
Host string `ini:"host"`
URLPrefix string `ini:"url_prefix"`
Host string `ini:"host"`
URLPrefix string `ini:"url_prefix"`
PprofEnabled bool `ini:"pprof_enabled"`
}
type DatabaseConfig struct {
@@ -46,6 +47,15 @@ func getEnvStr(key string, target *string) {
}
}
// getEnvBool 获取环境变量布尔值
func getEnvBool(key string, target *bool) {
if v := os.Getenv(key); v != "" {
if b, err := strconv.ParseBool(v); err == nil {
*target = b
}
}
}
// getEnvInt 获取环境变量整数
func getEnvInt(key string, target *int) {
if v := os.Getenv(key); v != "" {
@@ -59,8 +69,9 @@ func LoadConfig(path string) (*AppConfig, error) {
// 初始化默认配置
Config = &AppConfig{
Server: ServerConfig{
Port: 8052,
Host: "0.0.0.0",
Port: 8052,
Host: "0.0.0.0",
PprofEnabled: false,
},
Database: DatabaseConfig{
Type: "sqlite",
@@ -128,6 +139,7 @@ func applyEnvOverrides() {
getEnvInt("BH_SERVER_PORT", &Config.Server.Port)
getEnvStr("BH_SERVER_HOST", &Config.Server.Host)
getEnvStr("BH_SERVER_URL_PREFIX", &Config.Server.URLPrefix)
getEnvBool("BH_SERVER_PPROF", &Config.Server.PprofEnabled)
// Database
getEnvStr("BH_DB_TYPE", &Config.Database.Type)