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
+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)