-
Notifications
You must be signed in to change notification settings - Fork 5
/
matcher.go
156 lines (134 loc) · 3.03 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package gobis
import (
"encoding/json"
"fmt"
"github.com/gobwas/glob"
"regexp"
)
const (
PathRegex = "(?i)^((/[^/\\*]*)*)(/((\\*){1,2}))?$"
)
type PathMatcher struct {
pathMatcher *regexp.Regexp
expr string
appPath string
}
func NewPathMatcher(path string) *PathMatcher {
err := checkPathMatcher(path)
if err != nil {
panic(err)
}
return &PathMatcher{
pathMatcher: generatePathMatcher(path),
expr: path,
appPath: generateRawPath(path),
}
}
func (re PathMatcher) String() string {
return re.expr
}
func (re PathMatcher) AppPath() string {
return re.appPath
}
func (re PathMatcher) CreateRoutePath(finalPath string) string {
return re.appPath + finalPath
}
func (re *PathMatcher) UnmarshalCloud(data interface{}) error {
return re.load(data.(string))
}
func (re *PathMatcher) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
err := unmarshal(&s)
if err != nil {
return err
}
return re.load(s)
}
func (re *PathMatcher) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
return re.load(s)
}
func (re *PathMatcher) load(s string) error {
err := checkPathMatcher(s)
if err != nil {
return err
}
re.pathMatcher = generatePathMatcher(s)
re.expr = s
re.appPath = generateRawPath(s)
return nil
}
func checkPathMatcher(path string) error {
reg := regexp.MustCompile(PathRegex)
if !reg.MatchString(path) {
return fmt.Errorf("invalid path, e.g.: /api/** to match everything, /api/* to match first level or /api to only match this")
}
return nil
}
func generateRawPath(path string) string {
reg := regexp.MustCompile(PathRegex)
sub := reg.FindStringSubmatch(path)
return sub[1]
}
func generatePathMatcher(path string) *regexp.Regexp {
var pathMatcher *regexp.Regexp
reg := regexp.MustCompile(PathRegex)
sub := reg.FindStringSubmatch(path)
muxRoute := regexp.QuoteMeta(sub[1])
globSub := sub[4]
switch globSub {
case "*":
pathMatcher = regexp.MustCompile(fmt.Sprintf("^%s(/[^/]*)?$", muxRoute))
case "**":
pathMatcher = regexp.MustCompile(fmt.Sprintf("^%s(/.*)?$", muxRoute))
default:
pathMatcher = regexp.MustCompile(fmt.Sprintf("^%s$", muxRoute))
}
return pathMatcher
}
type HostMatchers []*HostMatcher
func (m HostMatchers) Match(s string) bool {
for _, matcher := range m {
if matcher.Match(s) {
return true
}
}
return false
}
type HostMatcher struct {
glob.Glob
raw string
}
func NewHostMatcher(hostOrWildcard string) *HostMatcher {
return &HostMatcher{
Glob: glob.MustCompile(hostOrWildcard, '.'),
raw: hostOrWildcard,
}
}
func (re *HostMatcher) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
err := unmarshal(&s)
if err != nil {
return err
}
re.Glob, err = glob.Compile(s, '.')
re.raw = s
return err
}
func (re *HostMatcher) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
re.Glob, err = glob.Compile(s, '.')
re.raw = s
return err
}
func (re HostMatcher) String() string {
return re.raw
}