-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
156 lines (129 loc) · 3.15 KB
/
route.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
package servicerouter
import (
"golang.org/x/net/context"
"regexp"
"strings"
"sync"
)
type routeMatcherFunc func(string) (string, bool)
func (f routeMatcherFunc) match(p string) (string, bool) {
r, e := f(p)
return r, e
}
func simplePrefixMatcher(prefix string) routeMatcherFunc {
return func(path string) (string, bool) {
nprefix := strings.TrimPrefix(path, prefix)
return nprefix, path != nprefix
}
}
func simpleMatcher(patten string) routeMatcherFunc {
return func(path string) (string, bool) {
return "", patten == path
}
}
func regexpMatcher(re *regexp.Regexp) routeMatcherFunc {
return func(path string) (string, bool) {
if idx := re.FindStringIndex(path); idx != nil {
return path[idx[1]:], true
}
return "", false
}
}
// Handler callback on matched route
type Handler interface {
Handle(context.Context, interface{}) (interface{}, error)
}
// HandlerFunc simplified Handler interface
type HandlerFunc func(context.Context, interface{}) (interface{}, error)
// Handle implementation of RoutedHandler
func (f HandlerFunc) Handle(ctx context.Context, req interface{}) (r interface{}, e error) {
r, e = f(ctx, req)
return
}
// Route holds a match
type Route struct {
mu sync.Mutex
name string
matcher routeMatcher
sub []*Route
h Handler
}
// Name name of route. just for logging and debuging
func (r *Route) Name() string {
return r.name
}
// AddRoute add a branch to this routing three
func (r *Route) AddRoute(opts ...RouteOptFn) *Route {
r.mu.Lock()
defer r.mu.Unlock()
s := newRoute(opts...)
r.sub = append(r.sub, s)
return s
}
func (r *Route) matchRoute(path string) (*Route, bool) {
r.mu.Lock()
defer r.mu.Unlock()
if r.matcher == nil || path == "" {
return nil, false // this route would never match anything
}
if extra, matched := r.matcher.match(path); matched {
if r.sub != nil && extra != "" {
for _, sroute := range r.sub {
if nroute, ok := sroute.matchRoute(extra); ok {
return nroute, true
}
}
}
if r.h != nil {
return r, true
}
}
return nil, false
}
type RouteOptFn func(*Route)
func newRoute(opts ...RouteOptFn) *Route {
s := &Route{
name: "UnNamed Route",
sub: make([]*Route, 0),
}
for _, opt := range opts {
opt(s)
}
return s
}
// SimpleRoute litral string matching ie: home.matching == home.matching
func SimpleRoute(path string) RouteOptFn {
return func(r *Route) {
r.matcher = simpleMatcher(path)
}
}
// PrefixRoute string prefix matching home. == home.matching
func PrefixRoute(prefix string) RouteOptFn {
return func(r *Route) {
r.matcher = simplePrefixMatcher(prefix)
}
}
// RegExpRoute regular expression based matching
func RegExpRoute(re *regexp.Regexp) RouteOptFn {
return func(r *Route) {
r.matcher = regexpMatcher(re)
}
}
// Name set name. for debuging purposes only
func Name(name string) RouteOptFn {
return func(r *Route) {
r.name = name
}
}
// RouteHandler set callback to execute if route matches
func RouteHandler(f Handler) RouteOptFn {
return func(r *Route) {
r.h = f
}
}
// RouteHandlerFunc set callback to execute if route matches
func RouteHandlerFunc(f HandlerFunc) RouteOptFn {
return func(r *Route) {
r.h = f
}
}