-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
router.go
219 lines (189 loc) · 5.32 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
214
215
216
217
218
219
package mux
import (
"errors"
"fmt"
"io"
"sync"
"github.com/plgd-dev/go-coap/v2/message"
"github.com/plgd-dev/go-coap/v2/message/codes"
)
type ResponseWriter = interface {
SetResponse(code codes.Code, contentFormat message.MediaType, d io.ReadSeeker, opts ...message.Option) error
Client() Client
}
type Handler interface {
ServeCOAP(w ResponseWriter, r *Message)
}
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as COAP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler object that calls f.
type HandlerFunc func(w ResponseWriter, r *Message)
type ErrorFunc = func(error)
// ServeCOAP calls f(w, r).
func (f HandlerFunc) ServeCOAP(w ResponseWriter, r *Message) {
f(w, r)
}
// Router is an COAP request multiplexer. It matches the
// path name of each incoming request against a list of
// registered patterns add calls the handler for the pattern
// with same name.
// Router is also safe for concurrent access from multiple goroutines.
type Router struct {
middlewares []MiddlewareFunc
errors ErrorFunc
m *sync.RWMutex
defaultHandler Handler // guarded by m
z map[string]Route // guarded by m
}
type Route struct {
h Handler
pattern string
regexMatcher *routeRegexp
}
func (route *Route) GetRouteRegexp() (string, error) {
if route.regexMatcher.regexp == nil {
return "", errors.New("mux: route does not have a regexp")
}
return route.regexMatcher.regexp.String(), nil
}
// NewRouter allocates and returns a new Router.
func NewRouter() *Router {
router := &Router{
middlewares: make([]MiddlewareFunc, 0, 2),
errors: func(err error) {
fmt.Println(err)
},
m: new(sync.RWMutex),
z: make(map[string]Route),
}
router.defaultHandler = HandlerFunc(func(w ResponseWriter, m *Message) {
if err := w.SetResponse(codes.NotFound, message.TextPlain, nil); err != nil {
router.errors(fmt.Errorf("router handler: cannot set response: %w", err))
}
})
return router
}
// Does path match pattern?
func pathMatch(pattern Route, path string) bool {
return pattern.regexMatcher.regexp.MatchString(path)
}
// Find a handler on a handler map given a path string
// Most-specific (longest) pattern wins
func (r *Router) Match(path string, routeParams *RouteParams) (matchedRoute *Route, matchedPattern string) {
r.m.RLock()
var n = 0
for pattern, route := range r.z {
if !pathMatch(route, path) {
continue
}
if matchedRoute == nil || len(pattern) > n {
n = len(pattern)
r := route
matchedRoute = &r
matchedPattern = pattern
}
}
r.m.RUnlock()
if matchedRoute == nil {
return
}
routeParams.Path = path
if routeParams.Vars == nil {
routeParams.Vars = make(map[string]string)
}
routeParams.PathTemplate = matchedPattern
matchedRoute.regexMatcher.extractRouteParams(path, routeParams)
return
}
// Handle adds a handler to the Router for pattern.
func (r *Router) Handle(pattern string, handler Handler) error {
switch pattern {
case "", "/":
pattern = "/"
}
if handler == nil {
return errors.New("nil handler")
}
routeRegex, err := newRouteRegexp(pattern)
if err != nil {
return err
}
r.m.Lock()
r.z[pattern] = Route{h: handler, pattern: pattern, regexMatcher: routeRegex}
r.m.Unlock()
return nil
}
// DefaultHandle set default handler to the Router
func (r *Router) DefaultHandle(handler Handler) {
r.m.Lock()
defer r.m.Unlock()
r.defaultHandler = handler
}
// HandleFunc adds a handler function to the Router for pattern.
func (r *Router) HandleFunc(pattern string, handler func(w ResponseWriter, r *Message)) {
if err := r.Handle(pattern, HandlerFunc(handler)); err != nil {
r.errors(fmt.Errorf("cannot handle pattern(%v): %w", pattern, err))
}
}
// DefaultHandleFunc set a default handler function to the Router.
func (r *Router) DefaultHandleFunc(handler func(w ResponseWriter, r *Message)) {
r.DefaultHandle(HandlerFunc(handler))
}
// HandleRemove deregistrars the handler specific for pattern from the Router.
func (r *Router) HandleRemove(pattern string) error {
switch pattern {
case "", "/":
pattern = "/"
}
r.m.Lock()
defer r.m.Unlock()
if _, ok := r.z[pattern]; ok {
delete(r.z, pattern)
return nil
}
return errors.New("pattern is not registered in")
}
// GetRoute obtains route from the pattern it has been assigned
func (r *Router) GetRoute(pattern string) *Route {
r.m.RLock()
defer r.m.RUnlock()
if route, ok := r.z[pattern]; ok {
return &route
}
return nil
}
func (r *Router) GetRoutes() map[string]Route {
r.m.RLock()
defer r.m.RUnlock()
return r.z
}
// ServeCOAP dispatches the request to the handler whose
// pattern most closely matches the request message. If DefaultServeMux
// is used the correct thing for DS queries is done: a possible parent
// is sought.
// If no handler is found a standard NotFound message is returned
func (r *Router) ServeCOAP(w ResponseWriter, req *Message) {
path, err := req.Options.Path()
r.m.RLock()
defaultHandler := r.defaultHandler
r.m.RUnlock()
if err != nil {
defaultHandler.ServeCOAP(w, req)
return
}
var h Handler
matchedMuxEntry, _ := r.Match(path, req.RouteParams)
if matchedMuxEntry == nil {
h = defaultHandler
} else {
h = matchedMuxEntry.h
}
if h == nil {
return
}
for i := len(r.middlewares) - 1; i >= 0; i-- {
h = r.middlewares[i].Middleware(h)
}
h.ServeCOAP(w, req)
}