-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
120 lines (97 loc) · 2.68 KB
/
registry.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
package chain
import (
"log/slog"
"strings"
)
// Registry is an algorithm-independent framework for recording routes. This division allows us to explore different
// algorithms without breaking the contract.
type Registry struct {
canBeStatic [2048]bool
storage *RouteStorage
routes []*Route
middlewares []*Middleware
static map[string]*Route
}
func (r *Registry) findHandle(ctx *Context) *Route {
if r.canBeStatic[len(ctx.path)] {
if route, found := r.static[ctx.path]; found {
return route
}
}
if r.storage == nil {
return nil
}
return r.storage.lookup(ctx)
}
func (r *Registry) findHandleCaseInsensitive(ctx *Context) *Route {
if r.canBeStatic[len(ctx.path)] {
for key, route := range r.static {
if strings.EqualFold(ctx.path, key) {
return route
}
}
}
if r.storage == nil {
return nil
}
return r.storage.lookupCaseInsensitive(ctx)
}
func (r *Registry) addHandle(path string, handle Handle) {
if r.routes == nil {
r.routes = []*Route{}
}
details := ParseRouteInfo(path)
// avoid conflicts
for _, route := range r.routes {
if details.conflictsWith(route.Info) {
slog.Error("[chain] wildcard conflicts", slog.String("new", details.path), slog.String("existing", route.Info.path))
panic("[chain] wildcard conflicts")
}
}
if !details.hasParameter && !details.hasWildcard {
if r.static == nil {
r.static = map[string]*Route{}
}
r.canBeStatic[len(path)] = true
r.static[path] = r.createRoute(handle, details)
return
}
if r.storage == nil {
r.storage = &RouteStorage{}
}
r.storage.add(r.createRoute(handle, details))
}
func (r *Registry) createRoute(handle Handle, info *RouteInfo) *Route {
route := &Route{
Handle: handle,
Info: info,
middlewaresAdded: map[*Middleware]bool{},
}
r.routes = append(r.routes, route)
for _, middleware := range r.middlewares {
if route.middlewaresAdded[middleware] != true && middleware.Path.Matches(route.Info) {
route.middlewaresAdded[middleware] = true
route.Middlewares = append(route.Middlewares, middleware)
}
}
return route
}
func (r *Registry) addMiddleware(path string, middlewares []func(ctx *Context, next func() error) error) {
if r.middlewares == nil {
r.middlewares = []*Middleware{}
}
for _, middleware := range middlewares {
info := &Middleware{
Path: ParseRouteInfo(path),
Handle: middleware,
}
r.middlewares = append(r.middlewares, info)
// add this MiddlewareFunc to all compatible routes
for _, route := range r.routes {
if route.middlewaresAdded[info] != true && info.Path.Matches(route.Info) {
route.middlewaresAdded[info] = true
route.Middlewares = append(route.Middlewares, info)
}
}
}
}