优化配置管理逻辑,确保路径配置的扩展名规则在更新时得到正确处理,移除冗余代码以简化回调逻辑。

This commit is contained in:
wood chen 2025-05-01 08:42:26 +08:00
parent 1d84c0c614
commit 4156b64ac6
4 changed files with 74 additions and 14 deletions

1
.gitignore vendored
View File

@ -33,7 +33,6 @@ data/metrics/metrics.json
data/metrics/path_stats.json
data/metrics/referer_stats.json
data/metrics/status_codes.json
data/config.example.json
data/config.json
.env
data/cache

61
data/config.example.json Normal file
View File

@ -0,0 +1,61 @@
{
"MAP": {
"/path1": {
"DefaultTarget": "https://path1.com/path/path/path",
"ExtensionMap": [
{
"Extensions": "jpg,png,avif",
"Target": "https://path1-img.com/path/path/path",
"SizeThreshold": 204800,
"MaxSize": 5242880
},
{
"Extensions": "mp4,webm",
"Target": "https://path1-video.com/path/path/path",
"SizeThreshold": 204800,
"MaxSize": 5242880
},
{
"Extensions": "*",
"Target": "https://path1-wildcard.com/path/path/path",
"SizeThreshold": 204800,
"MaxSize": 5242880
}
]
},
"/path2": "https://path2.com",
"/path3": {
"DefaultTarget": "https://path3.com",
"ExtensionMap": [
{
"Extensions": "*",
"Target": "https://path3-wildcard.com",
"SizeThreshold": 512000,
"MaxSize": 10485760
}
],
"SizeThreshold": 512000
},
"/wildcard-no-limits": {
"DefaultTarget": "https://default.example.com",
"ExtensionMap": [
{
"Extensions": "*",
"Target": "https://unlimited.example.com",
"SizeThreshold": 0,
"MaxSize": 0
}
]
}
},
"Compression": {
"Gzip": {
"Enabled": false,
"Level": 6
},
"Brotli": {
"Enabled": false,
"Level": 4
}
}
}

View File

@ -41,8 +41,9 @@ func NewConfigManager(configPath string) (*ConfigManager, error) {
}
// 确保所有路径配置的扩展名规则都已更新
for _, pathConfig := range config.MAP {
pathConfig.ProcessExtensionMap()
for path, pc := range config.MAP {
pc.ProcessExtensionMap()
config.MAP[path] = pc // 更新回原始map
}
cm.config.Store(config)
@ -131,8 +132,9 @@ func RegisterUpdateCallback(callback func(*Config)) {
// TriggerCallbacks 触发所有回调
func TriggerCallbacks(cfg *Config) {
// 确保所有路径配置的扩展名规则都已更新
for _, pathConfig := range cfg.MAP {
pathConfig.ProcessExtensionMap()
for path, pc := range cfg.MAP {
pc.ProcessExtensionMap()
cfg.MAP[path] = pc // 更新回原始map
}
callbackMutex.RLock()
@ -151,8 +153,9 @@ func (c *configImpl) Update(newConfig *Config) {
defer c.Unlock()
// 确保所有路径配置的扩展名规则都已更新
for _, pathConfig := range newConfig.MAP {
pathConfig.ProcessExtensionMap()
for path, pc := range newConfig.MAP {
pc.ProcessExtensionMap()
newConfig.MAP[path] = pc // 更新回原始map
}
// 更新配置
@ -191,8 +194,9 @@ func (cm *ConfigManager) loadConfig() error {
}
// 确保所有路径配置的扩展名规则都已更新
for _, pathConfig := range config.MAP {
pathConfig.ProcessExtensionMap()
for path, pc := range config.MAP {
pc.ProcessExtensionMap()
config.MAP[path] = pc // 更新回原始map
}
cm.config.Store(config)

View File

@ -179,11 +179,7 @@ func NewProxyHandler(cfg *config.Config) *ProxyHandler {
// 注册配置更新回调
config.RegisterUpdateCallback(func(newCfg *config.Config) {
// 确保所有路径配置的processedExtMap都已更新
for _, pathConfig := range newCfg.MAP {
pathConfig.ProcessExtensionMap()
}
// 注意config包已经在回调触发前处理了所有ExtRules这里无需再次处理
handler.pathMap = newCfg.MAP
handler.prefixTree.update(newCfg.MAP) // 更新前缀匹配树
handler.config = newCfg