-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
76 lines (60 loc) · 1.29 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
package npubo
type Node[T any] struct {
field *Node[T] // 上级节点
handler []HandlerFunc[T]
node map[rune]*Node[T]
}
type HandlerFunc[T any] func(*Context[T]) error
func newNode[T any](field *Node[T]) *Node[T] {
return &Node[T]{
field: field,
handler: []HandlerFunc[T]{},
node: make(map[rune]*Node[T]),
}
}
func (p *Node[T]) addNode(topic string, h HandlerFunc[T]) (*Node[T], int) {
parts := []rune(topic)
for i := 0; i < len(parts); i++ {
t := parts[i]
if t == '/' {
continue
}
// 路由结构是否存在
if _, ok := p.node[t]; !ok {
p.node[t] = newNode(p)
}
// 指向
p = p.node[t]
if t == '*' {
break
}
}
// 添加请求
p.handler = append(p.handler, h)
return p, len(p.handler) - 1
}
func (p *Node[T]) getRoute(topic string) []HandlerFunc[T] {
parts := []rune(topic)
handler := []HandlerFunc[T]{}
for i := 0; i < len(parts); i++ {
t := parts[i]
if t == '/' {
continue
}
// 查找以及通配
if v, ok := p.node['*']; ok {
handler = append(handler, v.handler...)
}
if _, ok := p.node[t]; !ok {
break
}
p = p.node[t]
handler = append(handler, p.handler...)
}
return handler
}
func (p *Node[T]) removeNode(id int) {
if len(p.handler) > id {
p.handler = append(p.handler[:id], p.handler[id+1:]...)
}
}