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", }, }, }