-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using "glob" matching, a backend server can register for "/foo.*" to handle routes such as "/foo.bar" and "/foo.bar.baz". The default option is still prefix matching (original behavior)
- Loading branch information
1 parent
501cc0d
commit c183ca0
Showing
8 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,42 @@ | ||
package route | ||
|
||
import "strings" | ||
import ( | ||
"strings" | ||
"path" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
// match contains the matcher function | ||
var match matcher = prefixMatcher | ||
|
||
// matcher determines whether a host/path matches a route | ||
type matcher func(path string, r *Route) bool | ||
type matcher func(uri string, r *Route) bool | ||
|
||
// prefixMatcher matches path to the routes' path. | ||
func prefixMatcher(path string, r *Route) bool { | ||
return strings.HasPrefix(path, r.Path) | ||
func prefixMatcher(uri string, r *Route) bool { | ||
return strings.HasPrefix(uri, r.Path) | ||
} | ||
|
||
// globMatcher matches path to the routes' path using globbing. | ||
func globMatcher(uri string, r *Route) bool { | ||
var hasMatch, err = path.Match(r.Path, uri) | ||
if err != nil { | ||
log.Print("[ERROR] Glob matching error %s for path %s route %s", err, uri, r.Path) | ||
return false | ||
} | ||
return hasMatch | ||
} | ||
|
||
// SetMatcher sets the matcher function for the proxy. | ||
func SetMatcher(s string) error { | ||
switch s { | ||
case "prefix": | ||
match = prefixMatcher | ||
case "glob": | ||
match = globMatcher | ||
default: | ||
return fmt.Errorf("route: invalid matcher: %s", s) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package route | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPrefixMatcher(t *testing.T) { | ||
routeFoo := newRoute("www.example.com", "/foo") | ||
|
||
tests := []struct { | ||
uri string | ||
want bool | ||
route *Route | ||
}{ | ||
{"/fo", false, routeFoo}, | ||
{"/foo", true, routeFoo}, | ||
{"/fools", true, routeFoo}, | ||
{"/bar", false, routeFoo}, | ||
} | ||
|
||
for _, tt := range tests { | ||
if got := prefixMatcher(tt.uri, tt.route); got != tt.want { | ||
t.Errorf("%s: got %v want %v", tt.uri, got, tt.want) | ||
} | ||
} | ||
} | ||
|
||
func TestGlobMatcher(t *testing.T) { | ||
routeFoo := newRoute("www.example.com", "/foo") | ||
routeFooWild := newRoute("www.example.com", "/foo.*") | ||
|
||
tests := []struct { | ||
uri string | ||
want bool | ||
route *Route | ||
}{ | ||
{"/fo", false, routeFoo}, | ||
{"/foo", true, routeFoo}, | ||
{"/fools", false, routeFoo}, | ||
{"/bar", false, routeFoo}, | ||
|
||
{"/fo", false, routeFooWild}, | ||
{"/foo", false, routeFooWild}, | ||
{"/fools", false, routeFooWild}, | ||
{"/foo.", true, routeFooWild}, | ||
{"/foo.a", true, routeFooWild}, | ||
{"/foo.bar", true, routeFooWild}, | ||
{"/foo.bar.baz", true, routeFooWild}, | ||
} | ||
|
||
for _, tt := range tests { | ||
if got := globMatcher(tt.uri, tt.route); got != tt.want { | ||
t.Errorf("%s: got %v want %v", tt.uri, got, tt.want) | ||
} | ||
} | ||
} |