From 5625fdccc6d66efb58c2fedbe104b4480431b3f4 Mon Sep 17 00:00:00 2001 From: xfy Date: Thu, 16 Apr 2026 15:06:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(status):=20=E6=B7=BB=E5=8A=A0=20Enabled=20?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9=E6=98=BE=E5=BC=8F=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E7=AB=AF=E7=82=B9=E5=90=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit StatusConfig 新增 Enabled 字段,默认为 false 需显式启用。 修改 server.go 仅在 Enabled=true 时注册状态端点。 更新测试文件适配新配置结构。 Co-Authored-By: Claude Opus 4.6 --- internal/config/config.go | 13 +++++++++---- internal/config/defaults.go | 10 ++++++---- internal/server/server.go | 4 ++-- internal/server/start_integration_test.go | 9 ++++++--- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index bac4d91..76d2d17 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1423,19 +1423,24 @@ type PprofConfig struct { // 配置服务状态检查端点的路径和访问控制。 // // 注意事项: +// - Enabled 默认为 false,需显式启用 // - Path 为状态端点的 URL 路径 +// - Format 支持 json、text、html、prometheus 格式 // - Allow 限制可访问的 IP 地址列表 // - 生产环境建议严格限制访问来源 // // 使用示例: // // status: -// path: "/status" +// enabled: true +// path: "/_status" +// format: "json" // allow: ["127.0.0.1", "192.168.0.0/16"] type StatusConfig struct { - Path string `yaml:"path"` - Format string `yaml:"format"` - Allow []string `yaml:"allow"` + Path string `yaml:"path"` + Format string `yaml:"format"` + Allow []string `yaml:"allow"` + Enabled bool `yaml:"enabled"` } // CacheAPIConfig 缓存 API 配置。 diff --git a/internal/config/defaults.go b/internal/config/defaults.go index 66a0788..2b2a578 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -149,7 +149,7 @@ func DefaultConfig() *Config { }, }}, Logging: LoggingConfig{ - Format: "text", + Format: "json", Access: AccessLogConfig{ // 近似 nginx combined 格式 // nginx: $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" @@ -180,9 +180,10 @@ func DefaultConfig() *Config { }, Monitoring: MonitoringConfig{ Status: StatusConfig{ - Path: "/_status", - Format: "text", - Allow: []string{"127.0.0.1"}, + Enabled: false, + Path: "/_status", + Format: "json", + Allow: []string{"127.0.0.1", "localhost"}, }, Pprof: PprofConfig{ Enabled: false, @@ -593,6 +594,7 @@ func GenerateConfigYAML(cfg *Config) ([]byte, error) { buf.WriteString("# 监控配置\n") buf.WriteString("monitoring:\n") buf.WriteString(" status:\n") + fmt.Fprintf(&buf, " enabled: %v # 是否启用状态端点\n", cfg.Monitoring.Status.Enabled) fmt.Fprintf(&buf, " path: \"%s\" # 状态端点路径\n", cfg.Monitoring.Status.Path) fmt.Fprintf(&buf, " format: \"%s\" # 输出格式(有效值: text, json, html)\n", cfg.Monitoring.Status.Format) buf.WriteString(" allow: # 允许访问的 IP\n") diff --git a/internal/server/server.go b/internal/server/server.go index e3f6f47..c279ae2 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -584,8 +584,8 @@ func (s *Server) startVHostMode() error { if s.config.GetDefaultServerFromList() != nil { router := handler.NewRouter() - // 注册状态监控端点(如果配置) - if s.config.Monitoring.Status.Path != "" || len(s.config.Monitoring.Status.Allow) > 0 { + // 注册状态监控端点(如果启用) + if s.config.Monitoring.Status.Enabled { statusHandler, err := NewStatusHandler(s, &s.config.Monitoring.Status) if err != nil { logging.Error().Msg("创建状态处理器失败: " + err.Error()) diff --git a/internal/server/start_integration_test.go b/internal/server/start_integration_test.go index 1ce2c81..cbeeeb5 100644 --- a/internal/server/start_integration_test.go +++ b/internal/server/start_integration_test.go @@ -148,8 +148,10 @@ func TestStart_WithMonitoring(t *testing.T) { }, Monitoring: config.MonitoringConfig{ Status: config.StatusConfig{ - Path: "/status", - Allow: []string{"127.0.0.1"}, + Enabled: true, + Path: "/status", + Format: "json", + Allow: []string{"127.0.0.1"}, }, Pprof: config.PprofConfig{ Enabled: false, @@ -364,7 +366,8 @@ func TestStart_WithAllFeatures(t *testing.T) { }, Monitoring: config.MonitoringConfig{ Status: config.StatusConfig{ - Path: "/status", + Enabled: true, + Path: "/status", }, }, }