-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
159 lines (134 loc) · 3.11 KB
/
router.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
package zen
import (
"net/http"
"strings"
)
type router struct {
tree map[string]*node
handlers map[string]HandlerFunc
}
func newRouter() *router {
return &router{
tree: make(map[string]*node),
handlers: make(map[string]HandlerFunc),
}
}
// Only one * is allowed
func (r *router) parsePattern(pattern string) []string {
vs := strings.Split(pattern, "/")
parts := make([]string, 0)
for _, item := range vs {
if item != "" {
parts = append(parts, item)
if item[0] == '*' {
break
}
}
}
return parts
}
func (r *router) Add(method, path string, handler HandlerFunc) {
r.addRoute(method, path, handler)
}
func (r *router) addRoute(method string, pattern string, handler HandlerFunc) {
parts := r.parsePattern(pattern)
key := method + "-" + pattern
_, ok := r.tree[method]
if !ok {
r.tree[method] = &node{}
}
r.tree[method].insert(pattern, parts, 0)
r.handlers[key] = handler
}
func (r *router) Find(method, path string) (*node, map[string]string) {
return r.getRoute(method, path)
}
func (r *router) getRoute(method string, path string) (*node, map[string]string) {
searchParts := r.parsePattern(path)
params := make(map[string]string)
root, ok := r.tree[method]
if !ok {
return nil, nil
}
n := root.find(searchParts, 0)
if n != nil {
parts := r.parsePattern(n.pattern)
for index, part := range parts {
if part[0] == ':' {
params[part[1:]] = searchParts[index]
}
if part[0] == '*' && len(part) > 1 {
params[part[1:]] = strings.Join(searchParts[index:], "/")
break
}
}
return n, params
}
return nil, nil
}
func (r *router) handle(c *Context) {
n, params := r.Find(c.method, c.path)
if n != nil {
key := c.method + "-" + n.pattern
c.params = params
c.middlewares = append(c.middlewares, r.handlers[key])
} else {
c.middlewares = append(c.middlewares, func(c *Context) {
c.String(http.StatusNotFound, "404 NOT FOUND: %s\n", c.path)
})
}
c.Next()
}
type node struct {
pattern string
part string
children []*node
isPrecision bool
}
func (n *node) matchChild(part string) *node {
for _, child := range n.children {
if child.part == part || child.isPrecision {
return child
}
}
return nil
}
func (n *node) matchChildren(part string) []*node {
nodes := make([]*node, 0)
for _, child := range n.children {
if child.part == part || child.isPrecision {
nodes = append(nodes, child)
}
}
return nodes
}
func (n *node) insert(pattern string, parts []string, height int) {
if len(parts) == height {
n.pattern = pattern
return
}
part := parts[height]
child := n.matchChild(part)
if child == nil {
child = &node{part: part, isPrecision: part[0] == ':' || part[0] == '*'}
n.children = append(n.children, child)
}
child.insert(pattern, parts, height+1)
}
func (n *node) find(parts []string, height int) *node {
if len(parts) == height || strings.HasPrefix(n.part, "*") {
if n.pattern == "" {
return nil
}
return n
}
part := parts[height]
children := n.matchChildren(part)
for _, child := range children {
result := child.find(parts, height+1)
if result != nil {
return result
}
}
return nil
}