-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
213 lines (192 loc) · 4.2 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @Author: dsreshiram@gmail.com
* @Date: 2022/7/16 下午 05:02
*/
package goweb
import (
"strings"
"sync/atomic"
)
type RouterGroup struct {
index uint32
middlewares []middleware
footMiddle []middleware
path string
method map[string]HandlerFunc
child map[string]*RouterGroup
parent *RouterGroup
// 记录路由添加顺序
order uint32 // 添加顺序
globalCount *uint32 // 全局计数
}
type middleware struct {
HandlerFunc
order uint32
}
func (g *RouterGroup) Grep(path string) *RouterGroup {
return g.position(path)
}
// Middleware 中间件,头部运行
func (g *RouterGroup) Middleware(handlers ...HandlerFunc) {
if len(g.middlewares) == 0 {
g.middlewares = make([]middleware, 0, len(handlers)+5)
}
for i := range handlers {
g.middlewares = append(g.middlewares, middleware{
HandlerFunc: handlers[i],
order: atomic.AddUint32(g.globalCount, 1), // 记录中间件添加时的位置
})
}
}
// FootMiddleware 最后运行的中间件
func (g *RouterGroup) FootMiddleware(handlers ...HandlerFunc) {
if len(g.footMiddle) == 0 {
g.footMiddle = make([]middleware, 0, len(handlers)+5)
}
for i := range handlers {
g.footMiddle = append(g.footMiddle, middleware{
HandlerFunc: handlers[i],
order: atomic.AddUint32(g.globalCount, 1), // 记录中间件添加时的位置
})
}
}
func (g *RouterGroup) position(path string) *RouterGroup {
for _, p := range strings.Split(path, "/") {
if p == "" {
continue
}
if _, ok := g.child[p]; ok {
g = g.child[p]
continue
}
if g.child == nil {
g.child = make(map[string]*RouterGroup, 10)
}
if p[0] == '#' {
if len(g.child) > 0 {
panic(path + " 地址泛匹配重复")
}
}
g.child[p] = &RouterGroup{
path: p,
parent: g,
index: g.index + 1,
globalCount: g.globalCount,
}
g = g.child[p]
if p == "#..." { // 后续全匹配
break
}
}
if g.globalCount == nil {
g.globalCount = new(uint32)
}
// 记录添加路由顺序
g.order = atomic.AddUint32(g.globalCount, 1)
return g
}
func (g *RouterGroup) completePath() string {
completePath := make([]string, g.index)
for g.index != 0 {
completePath[g.index-1] = g.path
g = g.parent
}
return "/" + strings.Join(completePath, "/")
}
func (g *RouterGroup) handle(method string, path string, handlerFunc HandlerFunc) {
g = g.position(path)
if method == ANY && len(g.method) > 1 {
if _, ok := g.method[ANY]; ok {
panic(g.completePath() + "该路由any方法冲突")
}
}
if _, ok := g.method[method]; ok {
panic(g.completePath() + "该路由method重复")
}
if g.method == nil {
g.method = make(map[string]HandlerFunc, 5)
}
g.method[method] = handlerFunc
}
func (g *RouterGroup) PathMatch(path, method string) (match map[string]string, handle HandlerFunc, grep *RouterGroup) {
var ok bool
var exit bool
splitPath := strings.Split(path, "/")
for i, p := range splitPath {
if exit {
break
}
if strings.Contains(p, "?") {
p = strings.Split(p, "?")[0]
exit = true
if p == "" {
break
}
}
if p == "" {
continue
}
if _, ok = g.child[p]; ok {
g = g.child[p]
continue
}
var find bool
if c, ok := g.child["#..."]; ok {
g = c
if match == nil {
match = make(map[string]string)
}
match["#"] = strings.Join(splitPath[i:], "/")
break
}
for child := range g.child {
if child[0] == '#' {
if match == nil {
match = make(map[string]string)
}
match[child[1:]] = p
g = g.child[child]
find = true
break
}
}
if !find {
return
}
}
handle, ok = g.method[method]
if !ok {
for m := range g.method {
if m == ANY {
handle = g.method[m]
}
}
}
grep = g
return
}
func (g *RouterGroup) BottomNodeList() (sub []*RouterGroup) {
if len(g.child) == 0 {
return nil
}
for _, v := range g.child {
bottoms := v.BottomNodeList()
if len(bottoms) == 0 {
sub = append(sub, v)
} else {
sub = append(sub, bottoms...)
}
}
return
}
func (g *RouterGroup) List() (method, path []string) {
buttomNode := g.BottomNodeList()
for _, b := range buttomNode {
p := b.completePath()
for m := range b.method {
method = append(method, m)
path = append(path, p)
}
}
return
}