-
Notifications
You must be signed in to change notification settings - Fork 10
/
router.go
125 lines (106 loc) · 2.69 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
package ziggurat
import (
"context"
"fmt"
"regexp"
"sort"
)
/*
Example route <bootstrap_server>/<topic>/<partition>
routerEntry {
handler ziggurat.Handler
pattern string
}
handlerEntry []string sorted by len of paths
*/
// routerEntry contains the pattern and the path routerEntry
type routerEntry struct {
handler Handler
pattern string
rgx *regexp.Regexp
}
type Router struct {
handlerEntry map[string]routerEntry
es []routerEntry
}
// match works by matching the shortest prefix that matches the path
// it returns the matched path and the handler associated with it
func (r *Router) match(path string) (Handler, string) {
if e, ok := r.handlerEntry[path]; ok {
return e.handler, path
}
for _, e := range r.es {
matched := e.rgx.MatchString(path)
if matched {
return e.handler, e.pattern
}
}
return nil, ""
}
func sortAndAppend(s []routerEntry, e routerEntry) []routerEntry {
n := len(s)
// get the insert position
// We are sorting all the patterns by len in descending order
i := sort.Search(n, func(i int) bool {
return len(e.pattern) > len(s[i].pattern)
})
s = append(s, routerEntry{})
copy(s[i+1:], s[i:])
s[i] = e
return s
}
func (r *Router) HandlerFunc(pattern string, h func(ctx context.Context, event *Event)) {
if pattern == "" {
panic(fmt.Errorf("kafka router:pattern cannot be [%q]", pattern))
}
if h == nil {
panic("kafka router:handler cannot be <nil>")
}
r.register(pattern, HandlerFunc(h))
}
func (r *Router) register(pattern string, h Handler) {
if r.handlerEntry == nil {
r.handlerEntry = make(map[string]routerEntry)
}
//check if pattern is `""` OR "/"
if (len(pattern) == 1 && pattern[len(pattern)-1] == '/') || pattern == "" {
panic(pattern + " is not a valid pattern")
}
//panic on multiple registrations
if _, ok := r.handlerEntry[pattern]; ok {
panic(fmt.Sprintf("kafka router:multiple regirstrations for [%s]", pattern))
}
e := routerEntry{handler: h, pattern: pattern, rgx: regexp.MustCompile(pattern)}
r.handlerEntry[pattern] = e
r.es = sortAndAppend(r.es, e)
}
func (r *Router) Handle(ctx context.Context, event *Event) {
path := event.RoutingPath
h, _ := r.match(path)
if h != nil {
h.Handle(ctx, event)
}
return
}
func NewRouter() *Router {
return &Router{}
}
type Middleware func(handler Handler) Handler
var pipe = func(h Handler, fs ...Middleware) Handler {
if len(fs) < 1 {
return h
}
last := len(fs) - 1
f := func(ctx context.Context, event *Event) {
next := h
for i := last; i >= 0; i-- {
next = fs[i](next)
}
next.Handle(ctx, event)
}
return HandlerFunc(f)
}
// Use takes a ziggurat.Handler and wraps it with Middleware
func Use(h Handler, fs ...Middleware) Handler {
return pipe(h, fs...)
}