-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
163 lines (138 loc) · 3.47 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
package gorouter
import (
"errors"
"net/url"
"strings"
)
var (
ErrInvalidMatcher = errors.New("invalid matcher")
ErrPathNotFound = errors.New("path not found")
)
type RouterNode[R any] struct {
Children []*RouterNode[R]
Matcher RouteMatcher[R]
Route map[string]Route[R]
RouteMatchers func() []RouteMatcher[R]
}
func NewRouter[R any]() *RouterNode[R] {
return &RouterNode[R]{
Children: make([]*RouterNode[R], 0, 1),
Matcher: &RouteMatcherRoot[R]{},
RouteMatchers: func() []RouteMatcher[R] {
return []RouteMatcher[R]{
&RouteMatcherPlaceholder[R]{},
&RouteMatcherString[R]{},
}
},
}
}
func (r *RouterNode[R]) AddRoute(route Route[R]) error {
return r.add(route, route.GetPath())
}
// Adds a route to RouterNode
func (r *RouterNode[R]) add(route Route[R], path string) error {
if r.Matcher == nil {
return ErrInvalidMatcher
}
// Make sure that we can get the token
// for the current matcher. "TokenMatch should
// return false when the token doesn't fit.
rem, ok := r.Matcher.TokenMatch(path, route)
if !ok {
return ErrInvalidMatcher
}
for _, c := range r.Children {
// If a child node is able to add then the
// route has been added. Otherwise, we need to add the route!
if err := c.add(route, rem); err == nil {
return nil
}
}
// Create matchers until there are no matchers left to create!
node := r
for rem != "" {
var matcher RouteMatcher[R]
var err error
// Add the route to myself by splitting into tokens!
matcher, rem, err = MatchPathToMatcher[R](rem, route, r.RouteMatchers())
if err != nil {
return err
}
newNode := &RouterNode[R]{
Children: make([]*RouterNode[R], 0, 1),
Matcher: matcher,
RouteMatchers: r.RouteMatchers,
}
node.Children = append(node.Children, newNode)
node = newNode
}
node.addLeaf(route)
return nil
}
func (r *RouterNode[R]) addLeaf(route Route[R]) {
if r.Route == nil {
r.Route = make(map[string]Route[R])
}
r.Route[route.GetMethod()] = route
}
func (r *RouterNode[R]) getLeaf(method string) Route[R] {
if r.Route == nil {
return nil
}
return r.Route[method]
}
type RouteParamList []string
func (r *RouteParamList) Add(param string) {
*r = append(*r, param)
}
func (r *RouterNode[R]) Match(method, path string, ctx *RouteContext) (Route[R], error) {
// If there is a # in the path, completely ignore it.
hashIdx := strings.Index(path, "#")
if hashIdx > -1 {
path = path[:hashIdx]
}
// If there is a ? in the path, parse separately.
queryIdx := strings.Index(path, "?")
if queryIdx > -1 {
//Ignore erroneous query strings.
ctx.Query, _ = url.ParseQuery(path[queryIdx+1:])
path = path[:queryIdx]
}
params := &RouteParamList{}
route, err := r.match(method, path, params)
if err != nil {
return nil, err
}
ctx.Params = make(map[string]string)
for i, p := range *params {
name := route.GetParamNames()[i]
ctx.Params[name] = p
}
return route, nil
}
func (r *RouterNode[R]) match(method, path string, params *RouteParamList) (Route[R], error) {
if r.Matcher == nil {
return nil, ErrInvalidMatcher
}
rem, ok := r.Matcher.Match(method, path, params)
if !ok {
return nil, ErrPathNotFound
}
if rem == "" {
route := r.getLeaf(method)
if route == nil {
return nil, ErrPathNotFound
}
return route, nil
}
for _, c := range r.Children {
r, err := c.match(method, rem, params)
if err == ErrInvalidMatcher {
return nil, ErrInvalidMatcher
}
if err == nil {
return r, nil
}
}
return nil, ErrPathNotFound
}