forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
42 lines (36 loc) · 932 Bytes
/
matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package route
import (
"fmt"
"log"
"path"
"strings"
)
// match contains the matcher function
var match matcher = prefixMatcher
// matcher determines whether a host/path matches a route
type matcher func(uri string, r *Route) bool
// prefixMatcher matches path to the routes' 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.Printf("[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
}