@@ -25,6 +25,8 @@ type Middleware interface {
2525 SetNext (m Middleware )
2626 Next (ctx Context ) error
2727 Exclude (routers ... string )
28+ HasExclude () bool
29+ ExistsExcludeRouter (router string ) bool
2830}
2931
3032//middleware 基础类,应用可基于此实现完整Moddleware
@@ -60,6 +62,12 @@ func (bm *BaseMiddlware) Next(ctx Context) error {
6062 return httpCtx .Handler ()(ctx )
6163 }
6264 } else {
65+ //check exclude config
66+ if ctx .RouterNode ().Node ().hasExcludeMiddleware && bm .next .HasExclude () {
67+ if bm .next .ExistsExcludeRouter (ctx .RouterNode ().Node ().fullPath ) {
68+ return bm .next .Next (ctx )
69+ }
70+ }
6371 return bm .next .Handle (ctx )
6472 }
6573 return nil
@@ -71,10 +79,29 @@ func (bm *BaseMiddlware) Exclude(routers ...string) {
7179 bm .excludeRouters = make (map [string ]struct {})
7280 }
7381 for _ , v := range routers {
74- if _ , exists := bm .excludeRouters [v ]; ! exists {
75- bm .excludeRouters [v ] = struct {}{}
76- }
82+ bm .excludeRouters [v ] = struct {}{}
83+ }
84+ }
85+
86+ // HasExclude check has set exclude router
87+ func (bm * BaseMiddlware ) HasExclude () bool {
88+ if bm .excludeRouters == nil {
89+ return false
90+ }
91+ if len (bm .excludeRouters ) > 0 {
92+ return true
93+ } else {
94+ return false
95+ }
96+ }
97+
98+ // ExistsExcludeRouter check is exists router in exclude map
99+ func (bm * BaseMiddlware ) ExistsExcludeRouter (router string ) bool {
100+ if bm .excludeRouters == nil {
101+ return false
77102 }
103+ _ , exists := bm .excludeRouters [router ]
104+ return exists
78105}
79106
80107type xMiddleware struct {
0 commit comments