-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
207 lines (186 loc) · 3.92 KB
/
router.go
File metadata and controls
207 lines (186 loc) · 3.92 KB
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
package router
type HandlerFunc func(params map[string]string)
type node struct {
prefix string
children []*node
paramName string
isParam bool
isWildcard bool // *
isMultiWild bool // **
handler HandlerFunc
}
type Tree struct {
staticRoutes map[string]HandlerFunc
root *node
}
type Router struct {
trees map[string]*Tree // method → tree
}
func New() *Router {
return &Router{
trees: make(map[string]*Tree),
}
}
func (r *Router) Register(method, path string, handler HandlerFunc) {
if _, ok := r.trees[method]; !ok {
r.trees[method] = &Tree{
staticRoutes: make(map[string]HandlerFunc),
root: &node{},
}
}
tree := r.trees[method]
if isStaticPath(path) {
tree.staticRoutes[path] = handler
return
}
insert(tree.root, path, handler)
}
func (r *Router) Match(method, path string) (HandlerFunc, map[string]string, bool) {
tree, ok := r.trees[method]
if !ok {
return nil, nil, false
}
if h, ok := tree.staticRoutes[path]; ok {
return h, nil, true
}
params := map[string]string{}
if h := match(tree.root, path, 0, params); h != nil {
return h, params, true
}
return nil, nil, false
}
// ---- internals ----
func isStaticPath(path string) bool {
for i := 0; i < len(path); i++ {
switch path[i] {
case '{', '*', '?':
return false
}
}
return true
}
func insert(n *node, path string, handler HandlerFunc) {
i := 0
for {
if i >= len(path) {
n.handler = handler
return
}
start := i
for i < len(path) && path[i] != '/' {
i++
}
seg := path[start:i]
if i < len(path) && path[i] == '/' {
i++
}
child := findChild(n, seg)
if child == nil {
child = &node{prefix: seg}
switch {
case seg == "*":
child.isWildcard = true
case seg == "**":
child.isMultiWild = true
case isParam(seg):
child.isParam = true
child.paramName = seg[1 : len(seg)-1]
}
n.children = append(n.children, child)
}
if child.isParam {
child.paramName = seg[1 : len(seg)-1]
}
n = child
}
}
func findChild(n *node, seg string) *node {
for _, ch := range n.children {
if ch.prefix == seg {
return ch
}
if ch.isParam && isParam(seg) && ch.paramName == seg[1:len(seg)-1] {
return ch
}
if ch.isWildcard && seg == "*" {
return ch
}
if ch.isMultiWild && seg == "**" {
return ch
}
}
return nil
}
func isParam(seg string) bool {
return len(seg) > 1 && seg[0] == '{' && seg[len(seg)-1] == '}'
}
func match(n *node, path string, i int, params map[string]string) HandlerFunc {
if i >= len(path) {
if n.handler != nil {
return n.handler
}
return nil
}
for _, ch := range n.children {
pi := i
newParams := copyParams(params) // 👈 param izolasyonu
switch {
case ch.isMultiWild:
for j := len(path); j >= pi; j-- {
if j < len(path) && path[j] != '/' {
continue
}
if h := match(ch, path, j, newParams); h != nil {
copyMap(params, newParams)
return h
}
}
case ch.isWildcard:
end := pi
for end < len(path) && path[end] != '/' {
end++
}
if h := match(ch, path, end+1, newParams); h != nil {
copyMap(params, newParams)
return h
}
case ch.isParam:
end := pi
for end < len(path) && path[end] != '/' {
end++
}
newParams[ch.paramName] = path[pi:end]
if h := match(ch, path, end+1, newParams); h != nil {
copyMap(params, newParams)
return h
}
default:
if len(path) >= pi+len(ch.prefix) && path[pi:pi+len(ch.prefix)] == ch.prefix {
next := pi + len(ch.prefix)
if next < len(path) && path[next] == '/' {
next++
}
if h := match(ch, path, next, newParams); h != nil {
copyMap(params, newParams)
return h
}
}
}
}
return nil
}
func copyParams(m map[string]string) map[string]string {
cp := make(map[string]string, len(m)+1)
for k, v := range m {
cp[k] = v
}
return cp
}
func copyMap(dst, src map[string]string) {
for k := range dst {
delete(dst, k)
}
for k, v := range src {
dst[k] = v
}
}