feat(status): 添加 Enabled 配置项显式控制状态端点启用

StatusConfig 新增 Enabled 字段,默认为 false 需显式启用。
修改 server.go 仅在 Enabled=true 时注册状态端点。
更新测试文件适配新配置结构。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-16 15:06:50 +08:00
parent 75317b44dd
commit 5625fdccc6
4 changed files with 23 additions and 13 deletions

View File

@ -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 配置。

View File

@ -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")

View File

@ -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())

View File

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