File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -31,3 +31,31 @@ func DisableCompression(internalFunc gin.HandlerFunc) gin.HandlerFunc {
3131}
3232
3333var _ MiddlewareFunc = DisableCompression
34+
35+ // MiddlewareChain 中间件链构建器
36+ type MiddlewareChain struct {
37+ middlewares []MiddlewareFunc
38+ }
39+
40+ // NewMiddlewareChain 创建新的中间件链
41+ func NewMiddlewareChain (middlewares ... MiddlewareFunc ) * MiddlewareChain {
42+ return & MiddlewareChain {
43+ middlewares : middlewares ,
44+ }
45+ }
46+
47+ // Add 添加中间件到链中
48+ func (mc * MiddlewareChain ) Add (middleware MiddlewareFunc ) * MiddlewareChain {
49+ mc .middlewares = append (mc .middlewares , middleware )
50+ return mc
51+ }
52+
53+ // Execute 执行中间件链
54+ func (mc * MiddlewareChain ) Execute (handler gin.HandlerFunc ) gin.HandlerFunc {
55+ // 从最后一个中间件开始,向前包装
56+ result := handler
57+ for i := len (mc .middlewares ) - 1 ; i >= 0 ; i -- {
58+ result = mc.middlewares [i ](result )
59+ }
60+ return result
61+ }
Original file line number Diff line number Diff line change @@ -52,6 +52,10 @@ func InitRouter() *gin.Engine {
5252 return ginR
5353}
5454
55+ var middlewareChain = NewMiddlewareChain ().
56+ Add (QueryKeyCaseInsensitive ).
57+ Add (DisableCompression )
58+
5559// 正则表达式路由处理器
5660//
5761// 从媒体服务器处理结构体中获取正则路由规则
@@ -63,7 +67,7 @@ func RegexpRouterHandler(ctx *gin.Context) {
6367 if rule .Regexp .MatchString (ctx .Request .URL .Path ) { // 不带查询参数的字符串:/emby/Items/54/Images/Primary
6468 logging .Debugf ("URL: %s 匹配成功 -> %s" , ctx .Request .URL .Path , rule .Regexp .String ())
6569
66- QueryKeyCaseInsensitive ( DisableCompression ( rule .Handler ) )(ctx )
70+ middlewareChain . Execute ( rule .Handler )(ctx )
6771 return
6872 }
6973 }
You can’t perform that action at this time.
0 commit comments