mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
refactor(config): optimize extension mapping handling and add utility methods
This commit is contained in:
parent
c2ba3e4d3d
commit
17f9692ce6
@ -3,8 +3,8 @@
|
||||
"/path1": {
|
||||
"DefaultTarget": "https://path1.com/path/path/path",
|
||||
"ExtensionMap": {
|
||||
"jpg": "https://path1-img.com/path/path/path",
|
||||
"png": "https://path1-img.com/path/path/path"
|
||||
"jpg,png,avif": "https://path1-img.com/path/path/path",
|
||||
"mp4,webm": "https://path1-video.com/path/path/path"
|
||||
}
|
||||
},
|
||||
"/path2": "https://path2.com",
|
||||
|
@ -1,6 +1,9 @@
|
||||
package config
|
||||
|
||||
import "encoding/json"
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
MAP map[string]PathConfig `json:"MAP"` // 改为使用PathConfig
|
||||
@ -9,8 +12,9 @@ type Config struct {
|
||||
}
|
||||
|
||||
type PathConfig struct {
|
||||
DefaultTarget string `json:"DefaultTarget"` // 默认回源地址
|
||||
ExtensionMap map[string]string `json:"ExtensionMap"` // 特定后缀的回源地址
|
||||
DefaultTarget string `json:"DefaultTarget"` // 默认回源地址
|
||||
ExtensionMap map[string]string `json:"ExtensionMap"` // 特定后缀的回源地址
|
||||
processedExtMap map[string]string // 内部使用,存储拆分后的映射
|
||||
}
|
||||
|
||||
type CompressionConfig struct {
|
||||
@ -51,9 +55,11 @@ func (c *Config) UnmarshalJSON(data []byte) error {
|
||||
// 尝试作为字符串解析
|
||||
var strValue string
|
||||
if err := json.Unmarshal(raw, &strValue); err == nil {
|
||||
c.MAP[key] = PathConfig{
|
||||
pathConfig := PathConfig{
|
||||
DefaultTarget: strValue,
|
||||
}
|
||||
pathConfig.ProcessExtensionMap() // 处理扩展名映射
|
||||
c.MAP[key] = pathConfig
|
||||
continue
|
||||
}
|
||||
|
||||
@ -62,6 +68,7 @@ func (c *Config) UnmarshalJSON(data []byte) error {
|
||||
if err := json.Unmarshal(raw, &pathConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
pathConfig.ProcessExtensionMap() // 处理扩展名映射
|
||||
c.MAP[key] = pathConfig
|
||||
}
|
||||
|
||||
@ -71,3 +78,32 @@ func (c *Config) UnmarshalJSON(data []byte) error {
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -79,9 +79,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ext := strings.ToLower(path.Ext(decodedPath))
|
||||
if ext != "" {
|
||||
ext = ext[1:] // 移除开头的点
|
||||
if alternativeTarget, exists := pathConfig.ExtensionMap[ext]; exists {
|
||||
targetBase = alternativeTarget
|
||||
}
|
||||
targetBase = pathConfig.GetTargetForExt(ext)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user