From e98b2c3efe0a3cad48f4fa8b45e04dcf7975d6d5 Mon Sep 17 00:00:00 2001 From: wood chen Date: Thu, 13 Mar 2025 13:23:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(config):=20=E6=9B=B4=E6=96=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 确保在更新配置时调用 ProcessExtensionMap 方法,以更新路径配置的 processedExtMap - 在配置更新和加载时添加日志记录,便于调试和监控 - 优化回调触发逻辑,确保所有路径配置在触发回调前已更新 --- internal/config/config.go | 24 ++++++++++++++++++++++++ internal/config/types.go | 3 +++ internal/handler/config.go | 15 +++++++++++++++ internal/handler/proxy.go | 7 ++++++- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/internal/config/config.go b/internal/config/config.go index b0191a8..029d750 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "encoding/json" + "log" "os" "strings" "sync" @@ -107,11 +108,19 @@ func RegisterUpdateCallback(callback func(*Config)) { // TriggerCallbacks 触发所有回调 func TriggerCallbacks(cfg *Config) { + // 确保所有路径配置的processedExtMap都已更新 + for _, pathConfig := range cfg.MAP { + pathConfig.ProcessExtensionMap() + } + callbackMutex.RLock() defer callbackMutex.RUnlock() for _, callback := range configCallbacks { callback(cfg) } + + // 添加日志 + log.Printf("[Config] 触发了 %d 个配置更新回调", len(configCallbacks)) } // Update 更新配置并触发回调 @@ -119,6 +128,11 @@ func (c *configImpl) Update(newConfig *Config) { c.Lock() defer c.Unlock() + // 确保所有路径配置的processedExtMap都已更新 + for _, pathConfig := range newConfig.MAP { + pathConfig.ProcessExtensionMap() + } + // 更新配置 c.MAP = newConfig.MAP c.Compression = newConfig.Compression @@ -127,6 +141,9 @@ func (c *configImpl) Update(newConfig *Config) { for _, callback := range c.onConfigUpdate { callback(newConfig) } + + // 添加日志 + log.Printf("[Config] 配置已更新: %d 个路径映射", len(newConfig.MAP)) } // reload 重新加载配置文件 @@ -150,7 +167,14 @@ func (cm *ConfigManager) loadConfig() error { if err != nil { return err } + + // 确保所有路径配置的processedExtMap都已更新 + for _, pathConfig := range config.MAP { + pathConfig.ProcessExtensionMap() + } + cm.config.Store(config) + log.Printf("[ConfigManager] 配置已加载: %d 个路径映射", len(config.MAP)) return nil } diff --git a/internal/config/types.go b/internal/config/types.go index 901142e..90bdf00 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -76,10 +76,13 @@ func (c *Config) UnmarshalJSON(data []byte) error { // 添加处理扩展名映射的方法 func (p *PathConfig) ProcessExtensionMap() { if p.ExtensionMap == nil { + p.processedExtMap = nil return } + // 重新创建processedExtMap,确保它是最新的 p.processedExtMap = make(map[string]string) + for exts, target := range p.ExtensionMap { // 分割扩展名 for _, ext := range strings.Split(exts, ",") { diff --git a/internal/handler/config.go b/internal/handler/config.go index 60fb8bb..131ee79 100644 --- a/internal/handler/config.go +++ b/internal/handler/config.go @@ -67,6 +67,11 @@ func (h *ConfigHandler) handleSaveConfig(w http.ResponseWriter, r *http.Request) return } + // 确保对每个路径配置调用ProcessExtensionMap方法 + for _, pathConfig := range newConfig.MAP { + pathConfig.ProcessExtensionMap() + } + // 将新配置格式化为JSON configData, err := json.MarshalIndent(newConfig, "", " ") if err != nil { @@ -89,8 +94,18 @@ func (h *ConfigHandler) handleSaveConfig(w http.ResponseWriter, r *http.Request) // 更新运行时配置 *h.config = newConfig + + // 确保在触发回调之前,所有路径配置的processedExtMap都已更新 + for _, pathConfig := range h.config.MAP { + pathConfig.ProcessExtensionMap() + } + + // 触发配置更新回调 config.TriggerCallbacks(h.config) + // 添加日志 + fmt.Printf("[Config] 配置已更新: %d 个路径映射\n", len(newConfig.MAP)) + w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"message": "配置已更新并生效"}`)) } diff --git a/internal/handler/proxy.go b/internal/handler/proxy.go index 586eb76..78382d7 100644 --- a/internal/handler/proxy.go +++ b/internal/handler/proxy.go @@ -118,9 +118,14 @@ func NewProxyHandler(cfg *config.Config) *ProxyHandler { // 注册配置更新回调 config.RegisterUpdateCallback(func(newCfg *config.Config) { + // 确保所有路径配置的processedExtMap都已更新 + for _, pathConfig := range newCfg.MAP { + pathConfig.ProcessExtensionMap() + } + handler.pathMap = newCfg.MAP handler.config = newCfg - log.Printf("[Config] 配置已更新并生效") + log.Printf("[Config] 代理处理器配置已更新: %d 个路径映射", len(newCfg.MAP)) }) return handler