mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
feat(config): 更新配置处理逻辑并添加日志记录
- 确保在更新配置时调用 ProcessExtensionMap 方法,以更新路径配置的 processedExtMap - 在配置更新和加载时添加日志记录,便于调试和监控 - 优化回调触发逻辑,确保所有路径配置在触发回调前已更新
This commit is contained in:
parent
50021c1a09
commit
e98b2c3efe
@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -107,11 +108,19 @@ func RegisterUpdateCallback(callback func(*Config)) {
|
|||||||
|
|
||||||
// TriggerCallbacks 触发所有回调
|
// TriggerCallbacks 触发所有回调
|
||||||
func TriggerCallbacks(cfg *Config) {
|
func TriggerCallbacks(cfg *Config) {
|
||||||
|
// 确保所有路径配置的processedExtMap都已更新
|
||||||
|
for _, pathConfig := range cfg.MAP {
|
||||||
|
pathConfig.ProcessExtensionMap()
|
||||||
|
}
|
||||||
|
|
||||||
callbackMutex.RLock()
|
callbackMutex.RLock()
|
||||||
defer callbackMutex.RUnlock()
|
defer callbackMutex.RUnlock()
|
||||||
for _, callback := range configCallbacks {
|
for _, callback := range configCallbacks {
|
||||||
callback(cfg)
|
callback(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加日志
|
||||||
|
log.Printf("[Config] 触发了 %d 个配置更新回调", len(configCallbacks))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update 更新配置并触发回调
|
// Update 更新配置并触发回调
|
||||||
@ -119,6 +128,11 @@ func (c *configImpl) Update(newConfig *Config) {
|
|||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
|
||||||
|
// 确保所有路径配置的processedExtMap都已更新
|
||||||
|
for _, pathConfig := range newConfig.MAP {
|
||||||
|
pathConfig.ProcessExtensionMap()
|
||||||
|
}
|
||||||
|
|
||||||
// 更新配置
|
// 更新配置
|
||||||
c.MAP = newConfig.MAP
|
c.MAP = newConfig.MAP
|
||||||
c.Compression = newConfig.Compression
|
c.Compression = newConfig.Compression
|
||||||
@ -127,6 +141,9 @@ func (c *configImpl) Update(newConfig *Config) {
|
|||||||
for _, callback := range c.onConfigUpdate {
|
for _, callback := range c.onConfigUpdate {
|
||||||
callback(newConfig)
|
callback(newConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加日志
|
||||||
|
log.Printf("[Config] 配置已更新: %d 个路径映射", len(newConfig.MAP))
|
||||||
}
|
}
|
||||||
|
|
||||||
// reload 重新加载配置文件
|
// reload 重新加载配置文件
|
||||||
@ -150,7 +167,14 @@ func (cm *ConfigManager) loadConfig() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 确保所有路径配置的processedExtMap都已更新
|
||||||
|
for _, pathConfig := range config.MAP {
|
||||||
|
pathConfig.ProcessExtensionMap()
|
||||||
|
}
|
||||||
|
|
||||||
cm.config.Store(config)
|
cm.config.Store(config)
|
||||||
|
log.Printf("[ConfigManager] 配置已加载: %d 个路径映射", len(config.MAP))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,10 +76,13 @@ func (c *Config) UnmarshalJSON(data []byte) error {
|
|||||||
// 添加处理扩展名映射的方法
|
// 添加处理扩展名映射的方法
|
||||||
func (p *PathConfig) ProcessExtensionMap() {
|
func (p *PathConfig) ProcessExtensionMap() {
|
||||||
if p.ExtensionMap == nil {
|
if p.ExtensionMap == nil {
|
||||||
|
p.processedExtMap = nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 重新创建processedExtMap,确保它是最新的
|
||||||
p.processedExtMap = make(map[string]string)
|
p.processedExtMap = make(map[string]string)
|
||||||
|
|
||||||
for exts, target := range p.ExtensionMap {
|
for exts, target := range p.ExtensionMap {
|
||||||
// 分割扩展名
|
// 分割扩展名
|
||||||
for _, ext := range strings.Split(exts, ",") {
|
for _, ext := range strings.Split(exts, ",") {
|
||||||
|
@ -67,6 +67,11 @@ func (h *ConfigHandler) handleSaveConfig(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 确保对每个路径配置调用ProcessExtensionMap方法
|
||||||
|
for _, pathConfig := range newConfig.MAP {
|
||||||
|
pathConfig.ProcessExtensionMap()
|
||||||
|
}
|
||||||
|
|
||||||
// 将新配置格式化为JSON
|
// 将新配置格式化为JSON
|
||||||
configData, err := json.MarshalIndent(newConfig, "", " ")
|
configData, err := json.MarshalIndent(newConfig, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -89,8 +94,18 @@ func (h *ConfigHandler) handleSaveConfig(w http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
// 更新运行时配置
|
// 更新运行时配置
|
||||||
*h.config = newConfig
|
*h.config = newConfig
|
||||||
|
|
||||||
|
// 确保在触发回调之前,所有路径配置的processedExtMap都已更新
|
||||||
|
for _, pathConfig := range h.config.MAP {
|
||||||
|
pathConfig.ProcessExtensionMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 触发配置更新回调
|
||||||
config.TriggerCallbacks(h.config)
|
config.TriggerCallbacks(h.config)
|
||||||
|
|
||||||
|
// 添加日志
|
||||||
|
fmt.Printf("[Config] 配置已更新: %d 个路径映射\n", len(newConfig.MAP))
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write([]byte(`{"message": "配置已更新并生效"}`))
|
w.Write([]byte(`{"message": "配置已更新并生效"}`))
|
||||||
}
|
}
|
||||||
|
@ -118,9 +118,14 @@ func NewProxyHandler(cfg *config.Config) *ProxyHandler {
|
|||||||
|
|
||||||
// 注册配置更新回调
|
// 注册配置更新回调
|
||||||
config.RegisterUpdateCallback(func(newCfg *config.Config) {
|
config.RegisterUpdateCallback(func(newCfg *config.Config) {
|
||||||
|
// 确保所有路径配置的processedExtMap都已更新
|
||||||
|
for _, pathConfig := range newCfg.MAP {
|
||||||
|
pathConfig.ProcessExtensionMap()
|
||||||
|
}
|
||||||
|
|
||||||
handler.pathMap = newCfg.MAP
|
handler.pathMap = newCfg.MAP
|
||||||
handler.config = newCfg
|
handler.config = newCfg
|
||||||
log.Printf("[Config] 配置已更新并生效")
|
log.Printf("[Config] 代理处理器配置已更新: %d 个路径映射", len(newCfg.MAP))
|
||||||
})
|
})
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
Loading…
x
Reference in New Issue
Block a user