From 327b4f4a2d947a88b893ae77d5e486a4418984ac Mon Sep 17 00:00:00 2001 From: tbxark Date: Tue, 5 Nov 2024 23:27:00 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E4=BB=B6=E9=80=89=E6=8B=A9=E5=99=A8=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/server/dash/web.go | 2 +- pkg/server/ginx/metadata.go | 4 ++-- pkg/server/middleware/selector/selector.go | 24 ++++++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/internal/server/dash/web.go b/internal/server/dash/web.go index 0b908c0..48ac4b2 100644 --- a/internal/server/dash/web.go +++ b/internal/server/dash/web.go @@ -86,7 +86,7 @@ func (w *Web) Run() error { selector.NewSelectorMiddleware( selector.MatchFunc( ginx.MatchOperation( - authRoute, + authRoute.BasePath(), dashv1.MetadataAuthService[:], dashv1.OperationAuthServiceAuthLogin, ), diff --git a/pkg/server/ginx/metadata.go b/pkg/server/ginx/metadata.go index 332eb6c..2ba80c6 100644 --- a/pkg/server/ginx/metadata.go +++ b/pkg/server/ginx/metadata.go @@ -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{}{} diff --git a/pkg/server/middleware/selector/selector.go b/pkg/server/middleware/selector/selector.go index 2bd8c52..bd027e5 100644 --- a/pkg/server/middleware/selector/selector.go +++ b/pkg/server/middleware/selector/selector.go @@ -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 }) }