Skip to content

Commit

Permalink
refactor(main): update handler matching logic to use function instead…
Browse files Browse the repository at this point in the history
… of prefix
  • Loading branch information
woodchen-ink committed Oct 31, 2024
1 parent b1b6a43 commit c2c6e14
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,35 @@ func main() {

// 创建处理器链
handlers := []struct {
pathPrefix string
handler http.Handler
matcher func(*http.Request) bool
handler http.Handler
}{
// 固定路径处理器
{
pathPrefix: "", // 空字符串表示检查所有 FixedPaths 配置
handler: middleware.FixedPathProxyMiddleware(cfg.FixedPaths)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
matcher: func(r *http.Request) bool {
for _, fp := range cfg.FixedPaths {
if strings.HasPrefix(r.URL.Path, fp.Path) {
return true
}
}
return false
},
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) {})),
// },
// 默认代理处理器放在最后
// 默认代理处理器
{
pathPrefix: "",
handler: proxyHandler,
matcher: func(r *http.Request) bool {
return true // 总是匹配,作为默认处理器
},
handler: proxyHandler,
},
}

// 创建主处理器
mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 遍历所有处理器
for _, h := range handlers {
if h.pathPrefix == "" || strings.HasPrefix(r.URL.Path, h.pathPrefix) {
if h.matcher(r) {
h.handler.ServeHTTP(w, r)
return
}
Expand Down

0 comments on commit c2c6e14

Please sign in to comment.