-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
187 lines (147 loc) · 3.71 KB
/
node.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package plugo
import (
"net/http"
"regexp"
)
// MidlewareFunc represents a function that is executed before or after an http request.
type MiddlewareFunc func(*error) http.HandlerFunc
// node represents a single route node of the tree.
type node struct {
// type of the node
kind nodeType
// pattern expression of the current node
label string
// regexp matcher for regexp nodes
rex *regexp.Regexp
// http handler endpoints
endpoints endpoints
// slice of middlewares to execute after a request
middlewares []MiddlewareFunc
// parent node
parent *node
// catch all nodes
catchAll *node
// parametric nodes
params *node
// slice with static and regexp nodes
children []*node
// slice of regexp node
matchers []*node
// slice of static node
statics []*node
// isLeaf indicates that node does not have child routes
isLeaf bool
// isHandler indicate if the node has any http handler func registered
isHandler bool
}
type nodeType uint8
const (
nodeStatic nodeType = iota // Ex. /home
nodeRegexp // Ex. /{[0-9]+}
nodeParam // Ex. /:user
nodeCatchAll // Ex. /api/*
)
func newNode(pattern string) *node {
if len(pattern) == 0 {
panic(ErrCreateEmptyNode)
}
var exp *regexp.Regexp
var err error
typ := parseStringToNodeType(pattern)
if typ == nodeRegexp {
if len(pattern) >= 2 {
exp, err = regexp.Compile(pattern[1 : len(pattern)-1])
if err != nil {
panic(ErrPatternNotCompile)
}
}
}
return &node{
kind: typ,
label: pattern,
rex: exp,
endpoints: make(endpoints),
catchAll: nil,
params: nil,
children: make([]*node, 0),
matchers: make([]*node, 0),
statics: make([]*node, 0),
}
}
func (nd *node) bind(mid MethodID, pattern string, handler http.Handler) {
nd.endpoints[mid] = &endpoint{
handler,
pattern,
}
nd.isHandler = true
}
func (nd *node) use(middlewares ...MiddlewareFunc) {
nd.middlewares = append(nd.middlewares, middlewares...)
}
func (nd *node) insertNode(label string) *node {
newElement := newNode(label)
newElement.isLeaf = true
newElement.parent = nd
nd.isLeaf = false
switch newElement.kind {
case nodeRegexp:
nd.matchers = append(nd.matchers, newElement)
nd.children = append(nd.children, newElement)
case nodeStatic:
nd.statics = append(nd.statics, newElement)
nd.children = append(nd.children, newElement)
case nodeParam:
// clear for generic parameter
newElement.label = ":"
// saves the data of the previous node before overwriting it
if nd.params != nil {
newElement.children = append(newElement.children, nd.children...)
newElement.matchers = append(newElement.matchers, nd.matchers...)
newElement.statics = append(newElement.statics, nd.statics...)
newElement.catchAll = nd.catchAll
newElement.endpoints = nd.endpoints
}
nd.params = newElement
case nodeCatchAll:
nd.catchAll = newElement
}
return newElement
}
// match checks for matches on static and regexp nodes.
func (nd *node) match(exp string) bool {
switch nd.kind {
case nodeStatic:
if nd.label == exp {
return true
}
case nodeRegexp:
if nd.rex != nil {
ok := nd.rex.MatchString(exp)
if ok {
return true
}
}
}
return false
}
func (nd *node) findRoute(search string) *node {
ok := nd.match(search)
if ok {
return nd
}
if len(nd.children) > 0 {
for _, child := range nd.children {
ok = child.match(search)
if ok {
return child
}
}
}
if nd.params != nil {
return nd.params
}
if nd.catchAll != nil {
return nd.catchAll
}
return nil
}