refactor(config): 提取常量并删除未使用代码

- 新增 DefaultPprofPath 常量替代硬编码路径
- 删除 validate.go 中未使用的 parseSize() 函数

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-10 11:20:27 +08:00
parent 931144dd08
commit a10377f76d
4 changed files with 8 additions and 63 deletions

View File

@ -27,6 +27,12 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
// 默认配置常量。
const (
// DefaultPprofPath pprof 端点的默认路径。
DefaultPprofPath = "/debug/pprof"
)
// Config 根配置结构,支持单服务器和多虚拟主机两种模式。 // Config 根配置结构,支持单服务器和多虚拟主机两种模式。
// //
// 包含服务器配置、日志配置、性能配置和监控配置等模块。 // 包含服务器配置、日志配置、性能配置和监控配置等模块。

View File

@ -168,7 +168,7 @@ func DefaultConfig() *Config {
}, },
Pprof: PprofConfig{ Pprof: PprofConfig{
Enabled: false, Enabled: false,
Path: "/debug/pprof", Path: DefaultPprofPath,
Allow: []string{"127.0.0.1"}, Allow: []string{"127.0.0.1"},
}, },
}, },

View File

@ -868,64 +868,3 @@ func validateVariables(v *VariablesConfig) error {
} }
return nil return nil
} }
// parseSize 解析大小字符串为字节数。
//
// 支持单位b, kb, mb, gb不区分大小写
// 纯数字默认为字节。
//
// 参数:
// - s: 大小字符串,如 "16KB", "1MB", "1024"
//
// 返回值:
// - int64: 字节数
// - error: 解析失败时返回错误
func parseSize(s string) (int64, error) {
s = strings.TrimSpace(s)
if s == "" {
return 0, errors.New("大小字符串不能为空")
}
// 提取数字部分和单位
var numStr string
var unit string
for i := len(s) - 1; i >= 0; i-- {
c := s[i]
if c >= '0' && c <= '9' || c == '.' {
numStr = s[:i+1]
unit = strings.ToLower(s[i+1:])
break
}
}
if numStr == "" {
return 0, fmt.Errorf("无效的大小格式: %s", s)
}
// 解析数字
var value float64
_, err := fmt.Sscanf(numStr, "%f", &value)
if err != nil {
return 0, fmt.Errorf("无法解析数字: %s", numStr)
}
// 转换单位
var multiplier int64
switch unit {
case "", "b":
multiplier = 1
case "k", "kb":
multiplier = 1024
case "m", "mb":
multiplier = 1024 * 1024
case "g", "gb":
multiplier = 1024 * 1024 * 1024
default:
return 0, fmt.Errorf("未知单位: %s", unit)
}
return int64(value * float64(multiplier)), nil
}
// unused: kept for potential future use in size parsing
var _ = parseSize

View File

@ -62,7 +62,7 @@ func NewPprofHandler(cfg *config.PprofConfig) (*PprofHandler, error) {
path := cfg.Path path := cfg.Path
if path == "" { if path == "" {
path = "/debug/pprof" path = config.DefaultPprofPath
} }
h := &PprofHandler{path: path} h := &PprofHandler{path: path}