-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.go
55 lines (47 loc) · 815 Bytes
/
tree.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
package ming
import (
"github.com/valyala/fasthttp"
)
type Tree []*Node
type Node struct {
method string
path string
handler fasthttp.RequestHandler
}
func (t *Tree) Add(n *Node) {
*t = append(*t, n)
}
func (t *Tree) FindMethod(method string) *Node {
for _, v := range *t {
if v.method == method {
return v
}
}
return nil
}
func (t *Tree) FindPath(path string) *Tree {
result := &Tree{}
for _, v := range *t {
if v.path == path {
result.Add(v)
}
}
return result
}
func (t *Tree) Len() int {
result := 0
for i := 0; i < len(*t); i++ {
result += 1
}
return result
}
func (n *Node) GetHandler() fasthttp.RequestHandler {
return n.handler
}
func (t *Tree) GetMethodAll() *Node {
if result := t.FindMethod("ALL"); result != nil {
return result
} else {
return nil
}
}