Skip to content

Commit

Permalink
chore: 优化中间件选择器封装
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 5, 2024
1 parent 5b82a11 commit 327b4f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/server/dash/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (w *Web) Run() error {
selector.NewSelectorMiddleware(
selector.MatchFunc(
ginx.MatchOperation(
authRoute,
authRoute.BasePath(),
dashv1.MetadataAuthService[:],
dashv1.OperationAuthServiceAuthLogin,
),
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/ginx/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func metadataToMatches(base string, metadata ...[][3]string) map[string]map[stri
return matches
}

func MatchOperation(route gin.IRouter, metadata [][3]string, operations ...string) func(ctx *gin.Context) bool {
matches := metadataToMatches(route.Group("").BasePath(), metadata)
func MatchOperation(base string, metadata [][3]string, operations ...string) func(ctx *gin.Context) bool {
matches := metadataToMatches(base, metadata)
opts := make(map[string]struct{}, len(operations))
for _, opt := range operations {
opts[opt] = struct{}{}
Expand Down
24 changes: 20 additions & 4 deletions pkg/server/middleware/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,31 @@ func NewContextMatcher[T comparable](key string, value T) Matcher {
})
}

func NewPathMatcher(path string) Matcher {
func NewLogicalNotMatcher(matcher Matcher) Matcher {
return MatchFunc(func(ctx *gin.Context) bool {
return ctx.FullPath() == path
return !matcher.Match(ctx)
})
}

func NewLogicalNotMatcher(matcher Matcher) Matcher {
func NewLogicalOrMatcher(matchers ...Matcher) Matcher {
return MatchFunc(func(ctx *gin.Context) bool {
return !matcher.Match(ctx)
for _, m := range matchers {
if m.Match(ctx) {
return true
}
}
return false
})
}

func NewLogicalAndMatcher(matchers ...Matcher) Matcher {
return MatchFunc(func(ctx *gin.Context) bool {
for _, m := range matchers {
if !m.Match(ctx) {
return false
}
}
return true
})
}

Expand Down

0 comments on commit 327b4f4

Please sign in to comment.