Skip to content

Commit 886a8fa

Browse files
committed
refactor: 重命名正则路由处理器函数并将中间件链封装到处理器内部
1 parent 00bc90e commit 886a8fa

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

internal/router/router.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,31 @@ func InitRouter() *gin.Engine {
4848
}
4949
}
5050

51-
ginR.NoRoute(RegexpRouterHandler)
51+
ginR.NoRoute(getRegexpRouterHandler())
5252
return ginR
5353
}
5454

55-
var middlewareChain = NewMiddlewareChain().
56-
Add(QueryKeyCaseInsensitive).
57-
Add(DisableCompression)
58-
5955
// 正则表达式路由处理器
6056
//
6157
// 从媒体服务器处理结构体中获取正则路由规则
6258
// 依次匹配请求, 找到对应的处理器
63-
func RegexpRouterHandler(ctx *gin.Context) {
59+
func getRegexpRouterHandler() gin.HandlerFunc {
6460
mediaServerHandler := handler.GetMediaServer()
61+
middlewareChain := NewMiddlewareChain().
62+
Add(QueryKeyCaseInsensitive).
63+
Add(DisableCompression)
6564

66-
for _, rule := range mediaServerHandler.GetRegexpRouteRules() {
67-
if rule.Regexp.MatchString(ctx.Request.URL.Path) { // 不带查询参数的字符串:/emby/Items/54/Images/Primary
68-
logging.Debugf("URL: %s 匹配成功 -> %s", ctx.Request.URL.Path, rule.Regexp.String())
65+
return func(ctx *gin.Context) {
66+
for _, rule := range mediaServerHandler.GetRegexpRouteRules() {
67+
if rule.Regexp.MatchString(ctx.Request.URL.Path) { // 不带查询参数的字符串:/emby/Items/54/Images/Primary
68+
logging.Debugf("URL: %s 匹配成功 -> %s", ctx.Request.URL.Path, rule.Regexp.String())
6969

70-
middlewareChain.Execute(rule.Handler)(ctx)
71-
return
70+
middlewareChain.Execute(rule.Handler)(ctx)
71+
return
72+
}
7273
}
73-
}
7474

75-
// 未匹配路由
76-
mediaServerHandler.ReverseProxy(ctx.Writer, ctx.Request)
75+
// 未匹配路由
76+
mediaServerHandler.ReverseProxy(ctx.Writer, ctx.Request)
77+
}
7778
}

0 commit comments

Comments
 (0)