refactor(config): optimize extension mapping handling and add utility methods

This commit is contained in:
wood chen 2024-11-17 08:24:57 +08:00
parent c2ba3e4d3d
commit 17f9692ce6
3 changed files with 43 additions and 9 deletions

View File

@ -3,8 +3,8 @@
"/path1": { "/path1": {
"DefaultTarget": "https://path1.com/path/path/path", "DefaultTarget": "https://path1.com/path/path/path",
"ExtensionMap": { "ExtensionMap": {
"jpg": "https://path1-img.com/path/path/path", "jpg,png,avif": "https://path1-img.com/path/path/path",
"png": "https://path1-img.com/path/path/path" "mp4,webm": "https://path1-video.com/path/path/path"
} }
}, },
"/path2": "https://path2.com", "/path2": "https://path2.com",

View File

@ -1,6 +1,9 @@
package config package config
import "encoding/json" import (
"encoding/json"
"strings"
)
type Config struct { type Config struct {
MAP map[string]PathConfig `json:"MAP"` // 改为使用PathConfig MAP map[string]PathConfig `json:"MAP"` // 改为使用PathConfig
@ -9,8 +12,9 @@ type Config struct {
} }
type PathConfig struct { type PathConfig struct {
DefaultTarget string `json:"DefaultTarget"` // 默认回源地址 DefaultTarget string `json:"DefaultTarget"` // 默认回源地址
ExtensionMap map[string]string `json:"ExtensionMap"` // 特定后缀的回源地址 ExtensionMap map[string]string `json:"ExtensionMap"` // 特定后缀的回源地址
processedExtMap map[string]string // 内部使用,存储拆分后的映射
} }
type CompressionConfig struct { type CompressionConfig struct {
@ -51,9 +55,11 @@ func (c *Config) UnmarshalJSON(data []byte) error {
// 尝试作为字符串解析 // 尝试作为字符串解析
var strValue string var strValue string
if err := json.Unmarshal(raw, &strValue); err == nil { if err := json.Unmarshal(raw, &strValue); err == nil {
c.MAP[key] = PathConfig{ pathConfig := PathConfig{
DefaultTarget: strValue, DefaultTarget: strValue,
} }
pathConfig.ProcessExtensionMap() // 处理扩展名映射
c.MAP[key] = pathConfig
continue continue
} }
@ -62,6 +68,7 @@ func (c *Config) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(raw, &pathConfig); err != nil { if err := json.Unmarshal(raw, &pathConfig); err != nil {
return err return err
} }
pathConfig.ProcessExtensionMap() // 处理扩展名映射
c.MAP[key] = pathConfig c.MAP[key] = pathConfig
} }
@ -71,3 +78,32 @@ func (c *Config) UnmarshalJSON(data []byte) error {
return nil return nil
} }
// 添加处理扩展名映射的方法
func (p *PathConfig) ProcessExtensionMap() {
if p.ExtensionMap == nil {
return
}
p.processedExtMap = make(map[string]string)
for exts, target := range p.ExtensionMap {
// 分割扩展名
for _, ext := range strings.Split(exts, ",") {
ext = strings.TrimSpace(ext) // 移除可能的空格
if ext != "" {
p.processedExtMap[ext] = target
}
}
}
}
// 添加获取目标URL的方法
func (p *PathConfig) GetTargetForExt(ext string) string {
if p.processedExtMap == nil {
p.ProcessExtensionMap()
}
if target, exists := p.processedExtMap[ext]; exists {
return target
}
return p.DefaultTarget
}

View File

@ -79,9 +79,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ext := strings.ToLower(path.Ext(decodedPath)) ext := strings.ToLower(path.Ext(decodedPath))
if ext != "" { if ext != "" {
ext = ext[1:] // 移除开头的点 ext = ext[1:] // 移除开头的点
if alternativeTarget, exists := pathConfig.ExtensionMap[ext]; exists { targetBase = pathConfig.GetTargetForExt(ext)
targetBase = alternativeTarget
}
} }
} }