-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
executable file
·162 lines (130 loc) · 3.15 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
package scaffold
import "sync"
type node struct {
lock sync.RWMutex
depth int
children map[string]*node
handlers map[string]Handler
middlware map[string][]Middleware
notfound map[string]Handler
}
func newNode(depth int) *node {
return &node{
depth: depth,
children: make(map[string]*node),
handlers: make(map[string]Handler),
middlware: make(map[string][]Middleware),
notfound: make(map[string]Handler),
}
}
func (n *node) node(parts []string, vars map[int]string) *node {
if len(parts) == 0 {
return n
}
next, parts := parts[0], parts[1:]
if next[0] == ':' {
vars[n.depth], next = next[1:], ""
}
n.lock.Lock()
if _, ok := n.children[next]; !ok {
n.children[next] = newNode(n.depth + 1)
}
n.lock.Unlock()
n.lock.RLock()
defer n.lock.RUnlock()
return n.children[next].node(parts, vars)
}
func (n *node) handle(method string, parts []string, handlers ...Handler) {
vars := make(map[int]string)
node := n.node(parts, vars)
node.lock.Lock()
node.handlers[method] = wrapHandler(vars, HandlerList(handlers))
node.lock.Unlock()
}
func (n *node) use(method string, parts []string, middleware ...Middleware) {
vars := make(map[int]string)
node := n.node(parts, vars)
m := wrapMiddleware(vars, MiddlewareList(middleware))
node.lock.Lock()
node.middlware[method] = append(node.middlware[method], m)
node.lock.Unlock()
}
func (n *node) error(method string, parts []string, handler Handler) {
vars := make(map[int]string)
node := n.node(parts, vars)
node.lock.Lock()
node.notfound[method] = wrapHandler(vars, handler)
node.lock.Unlock()
}
func (n *node) resolve(method string, parts []string) (Handler, []Middleware, bool) {
n.lock.RLock()
defer n.lock.RUnlock()
m1 := n.middleware(method)
if len(parts) == 0 {
if h, ok := n.handler(method); ok {
return h, m1, ok
}
return n.notFoundHandler(method, nil, nil), m1, false
}
next, parts := parts[0], parts[1:]
var h1, h2 Handler
var m2 []Middleware
var ok1, ok2 bool
if c, ok := n.children[next]; ok {
if h1, m2, ok1 = c.resolve(method, parts); ok {
m1 = append(m1, m2...)
}
}
if c, ok := n.children[""]; ok {
if h2, m2, ok2 = c.resolve(method, parts); ok {
m1 = append(m1, m2...)
}
}
if ok1 {
return h1, m1, true
}
if ok2 {
return h2, m1, true
}
return n.notFoundHandler(method, h1, h2), m1, false
}
func (n *node) handler(method string) (Handler, bool) {
n.lock.RLock()
defer n.lock.RUnlock()
if h, ok := n.handlers[method]; ok {
return h, true
}
if h, ok := n.handlers[""]; ok {
return h, true
}
return nil, false
}
func (n *node) notFoundHandler(method string, h1 Handler, h2 Handler) Handler {
n.lock.RLock()
defer n.lock.RUnlock()
if h1 != nil {
return h1
}
if h2 != nil {
return h2
}
if h, ok := n.notfound[method]; ok {
return h
}
if h, ok := n.notfound[""]; ok {
return h
}
return nil
}
func (n *node) middleware(method string) []Middleware {
n.lock.RLock()
defer n.lock.RUnlock()
middleware := make([]Middleware, 0, 10)
if m, ok := n.middlware[method]; ok {
middleware = append(middleware, m...)
}
if m, ok := n.middlware[""]; ok {
middleware = append(middleware, m...)
}
return middleware
}