-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoeTree.go
47 lines (41 loc) · 909 Bytes
/
GoeTree.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
package goe
type RouteInfo struct {
Method string
Path string
Handler string
HandlerFunc interface{}
}
type RoutesInfo []RouteInfo
type GoeTree struct {
trees methodTrees
}
func NewGoeTree() *GoeTree {
tree := &GoeTree{
trees: make(methodTrees, 0, 9),
}
return tree
}
func (this *GoeTree) addRoute(method, path string, handlers interface{}) {
root := this.trees.get(method)
if root == nil {
root = new(node)
root.fullPath = "/"
this.trees = append(this.trees, methodTree{method: method, root: root})
}
root.addRoute(path, handlers)
}
func (this *GoeTree) getRoute(httpMethod, path string) nodeValue {
t := this.trees
for i, tl := 0, len(t); i < tl; i++ {
if t[i].method != httpMethod {
continue
}
root := t[i].root
// Find route in tree
value := root.getValue(path, nil, false)
if value.handlers != nil {
return value
}
}
return nodeValue{}
}