Skip to content

Commit

Permalink
Add new & improved glob matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Shay Arbov committed Feb 28, 2018
1 parent 16805a9 commit f65d00b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
return nil, fmt.Errorf("invalid proxy.strategy: %s", cfg.Proxy.Strategy)
}

if cfg.Proxy.Matcher != "prefix" && cfg.Proxy.Matcher != "glob" {
if cfg.Proxy.Matcher != "prefix" && cfg.Proxy.Matcher != "glob" && cfg.Proxy.Matcher != "gobwas/glob" {
return nil, fmt.Errorf("invalid proxy.matcher: %s", cfg.Proxy.Matcher)
}

Expand Down
14 changes: 12 additions & 2 deletions route/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"log"
"path"
"strings"

"github.com/gobwas/glob"
)

// matcher determines whether a host/path matches a route
Expand All @@ -12,8 +14,9 @@ type matcher func(uri string, r *Route) bool
// Matcher contains the available matcher functions.
// Update config/load.go#load after updating.
var Matcher = map[string]matcher{
"prefix": prefixMatcher,
"glob": globMatcher,
"prefix": prefixMatcher,
"glob": globMatcher,
"gobwas/glob": gobwasGlobMatcher,
}

// prefixMatcher matches path to the routes' path.
Expand All @@ -30,3 +33,10 @@ func globMatcher(uri string, r *Route) bool {
}
return hasMatch
}


// gobwasGlobMatcher matches path to the routes' path using gobwas/glob.
func gobwasGlobMatcher(uri string, r *Route) bool {
var g = glob.MustCompile(r.Path)
return g.Match(uri)
}
33 changes: 33 additions & 0 deletions route/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,36 @@ func TestGlobMatcher(t *testing.T) {
})
}
}

func TestGobwasGlobMatcher(t *testing.T) {
tests := []struct {
uri string
matches bool
route *Route
}{
// happy flows
{uri: "/foo", matches: true, route: &Route{Path: "/foo"}},
{uri: "/fool", matches: true, route: &Route{Path: "/foo?"}},
{uri: "/fool", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/foo/x/bar", matches: true, route: &Route{Path: "/foo/*/bar"}},
{uri: "/foo/x/y/z/w/bar", matches: true, route: &Route{Path: "/foo/**"}},
{uri: "/foo/x/y/z/w/bar", matches: true, route: &Route{Path: "/foo/**/bar"}},

// error flows
{uri: "/fo", matches: false, route: &Route{Path: "/foo"}},
{uri: "/fools", matches: false, route: &Route{Path: "/foo"}},
{uri: "/fo", matches: false, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: false, route: &Route{Path: "/foo.*"}},
{uri: "/foo/x/y/z/w/baz", matches: false, route: &Route{Path: "/foo/**/bar"}},
}

for _, tt := range tests {
t.Run(tt.uri, func(t *testing.T) {
if got, want := gobwasGlobMatcher(tt.uri, tt.route), tt.matches; got != want {
t.Fatalf("got %v want %v", got, want)
}
})
}
}

0 comments on commit f65d00b

Please sign in to comment.