Skip to content

Commit

Permalink
feat(config, middleware): add fixed paths configuration and proxy mid…
Browse files Browse the repository at this point in the history
…dleware
  • Loading branch information
woodchen-ink committed Oct 30, 2024
1 parent 830eb86 commit 8f2c035
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
14 changes: 13 additions & 1 deletion data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,17 @@
"Enabled": false,
"Level": 4
}
}
},
"FixedPaths": [
{
"Path": "/cdnjs",
"TargetHost": "cdnjs.cloudflare.com",
"TargetURL": "https://cdnjs.cloudflare.com"
},
{
"Path": "/jsdelivr",
"TargetHost": "cdn.jsdelivr.net",
"TargetURL": "https://cdn.jsdelivr.net"
}
]
}
7 changes: 7 additions & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
type Config struct {
MAP map[string]string `json:"MAP"`
Compression CompressionConfig `json:"Compression"`
FixedPaths []FixedPathConfig `json:"FixedPaths"` // 新增
}

type CompressionConfig struct {
Expand All @@ -14,3 +15,9 @@ type CompressorConfig struct {
Enabled bool `json:"Enabled"`
Level int `json:"Level"`
}

type FixedPathConfig struct {
Path string `json:"Path"`
TargetHost string `json:"TargetHost"`
TargetURL string `json:"TargetURL"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"log"
"net"
"net/http"
"proxy-go/internal/config"
"strings"
)

type CDNJSConfig struct {
Path string // 固定路径,例如 "/cdnjs"
TargetHost string // 目标主机,例如 "cdnjs.cloudflare.com"
TargetURL string // 目标URL,例如 "https://cdnjs.cloudflare.com"
type FixedPathConfig struct {
Path string `json:"Path"`
TargetHost string `json:"TargetHost"`
TargetURL string `json:"TargetURL"`
}

// CDNJSMiddleware 处理固定路径的代理
func CDNJSMiddleware(configs []CDNJSConfig) func(http.Handler) http.Handler {
func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 检查是否匹配任何固定路径
Expand Down
39 changes: 21 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,40 @@ func main() {
// 创建代理处理器
proxyHandler := handler.NewProxyHandler(cfg.MAP)

// 创建 CDNJS 中间件配置
cdnjsConfigs := []middleware.CDNJSConfig{
// 创建处理器链
handlers := []struct {
pathPrefix string
handler http.Handler
}{
// 固定路径处理器
{
Path: "/cdnjs",
TargetHost: "cdnjs.cloudflare.com",
TargetURL: "https://cdnjs.cloudflare.com",
pathPrefix: "", // 空字符串表示检查所有 FixedPaths 配置
handler: middleware.FixedPathProxyMiddleware(cfg.FixedPaths)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
},
// 可以在这里添加其他固定路径处理器
// {
// pathPrefix: "/something",
// handler: someOtherMiddleware(config)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
// },
// 默认代理处理器放在最后
{
Path: "/jsdelivr",
TargetHost: "cdn.jsdelivr.net",
TargetURL: "https://cdn.jsdelivr.net",
pathPrefix: "",
handler: proxyHandler,
},
}

// 创建主处理器
mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 检查是否匹配任何固定路径配置
for _, cfg := range cdnjsConfigs {
if strings.HasPrefix(r.URL.Path, cfg.Path) {
// 使用 CDNJS 中间件处理
handler := middleware.CDNJSMiddleware(cdnjsConfigs)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
handler.ServeHTTP(w, r)
// 遍历所有处理器
for _, h := range handlers {
if h.pathPrefix == "" || strings.HasPrefix(r.URL.Path, h.pathPrefix) {
h.handler.ServeHTTP(w, r)
return
}
}

// 如果没有匹配的固定路径,使用普通代理处理器
proxyHandler.ServeHTTP(w, r)
})

// 对非 CDNJS 请求添加压缩中间件
// 添加压缩中间件
var handler http.Handler = mainHandler
if cfg.Compression.Gzip.Enabled || cfg.Compression.Brotli.Enabled {
handler = middleware.CompressionMiddleware(compManager)(handler)
Expand Down

0 comments on commit 8f2c035

Please sign in to comment.