From a10377f76df8a3c95c51e03fe052ea04949f4bc4 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 10 Apr 2026 11:20:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(config):=20=E6=8F=90=E5=8F=96=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E5=B9=B6=E5=88=A0=E9=99=A4=E6=9C=AA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 DefaultPprofPath 常量替代硬编码路径 - 删除 validate.go 中未使用的 parseSize() 函数 Co-Authored-By: Claude Opus 4.6 --- internal/config/config.go | 6 ++++ internal/config/defaults.go | 2 +- internal/config/validate.go | 61 ------------------------------------- internal/server/pprof.go | 2 +- 4 files changed, 8 insertions(+), 63 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 0036e0e..5262a01 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -27,6 +27,12 @@ import ( "gopkg.in/yaml.v3" ) +// 默认配置常量。 +const ( + // DefaultPprofPath pprof 端点的默认路径。 + DefaultPprofPath = "/debug/pprof" +) + // Config 根配置结构,支持单服务器和多虚拟主机两种模式。 // // 包含服务器配置、日志配置、性能配置和监控配置等模块。 diff --git a/internal/config/defaults.go b/internal/config/defaults.go index 408c6be..9bddffb 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -168,7 +168,7 @@ func DefaultConfig() *Config { }, Pprof: PprofConfig{ Enabled: false, - Path: "/debug/pprof", + Path: DefaultPprofPath, Allow: []string{"127.0.0.1"}, }, }, diff --git a/internal/config/validate.go b/internal/config/validate.go index 406a133..30253d5 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -868,64 +868,3 @@ func validateVariables(v *VariablesConfig) error { } 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 diff --git a/internal/server/pprof.go b/internal/server/pprof.go index a76b74e..34b083f 100644 --- a/internal/server/pprof.go +++ b/internal/server/pprof.go @@ -62,7 +62,7 @@ func NewPprofHandler(cfg *config.PprofConfig) (*PprofHandler, error) { path := cfg.Path if path == "" { - path = "/debug/pprof" + path = config.DefaultPprofPath } h := &PprofHandler{path: path}