-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
executable file
·216 lines (187 loc) · 5.33 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
package scaffold
import (
"net/http"
"path"
"golang.org/x/net/context"
)
// Router is a HTTP router
type Router struct {
route Route
dispatcher Dispatcher
builders []*func(interface{}) (Handler, error)
}
// New creates new router
func New(d Dispatcher) *Router {
return &Router{
dispatcher: d,
}
}
// Scaffold creates a router and passes it to a platorm
func Scaffold(d Dispatcher, platform Platform) Handler {
router := New(d)
platform.Routes(router)
return router.dispatcher
}
// Host lets you specify the host(s) the route is available for
func (r *Router) Host(hosts ...string) *Router {
c := r.route.clone()
c.Hosts = hosts
return r.clone(c)
}
// Route returns the subrouter for a pettern
func (r *Router) Route(pattern string) *Router {
c := r.pattern(pattern)
return r.clone(c)
}
// Group calls the specified function with the subrouter for the given pattern
func (r *Router) Group(pattern string, group func(*Router)) {
c := r.pattern(pattern)
group(r.clone(c))
}
// Platform routes the platform object to the given pattern
func (r *Router) Platform(pattern string, platform Platform) {
c := r.pattern(pattern)
platform.Routes(r.clone(c))
}
// Handle all methods with a given pattern
func (r *Router) Handle(pattern string, handlers ...interface{}) *Router {
c := r.pattern(pattern)
clone := r.clone(c)
clone.handle(handlers)
return clone
}
// Options handles OPTIONS methods with a given pattern
func (r *Router) Options(pattern string, handlers ...interface{}) *Router {
c := r.pattern(pattern)
c.Method = "OPTIONS"
clone := r.clone(c)
clone.handle(handlers)
return clone
}
// Get handles GET methods with a given pattern
func (r *Router) Get(pattern string, handlers ...interface{}) *Router {
c := r.pattern(pattern)
c.Method = "GET"
clone := r.clone(c)
clone.handle(handlers)
return clone
}
// Head handles HEAD methods with a given pattern
func (r *Router) Head(pattern string, handlers ...interface{}) *Router {
c := r.pattern(pattern)
c.Method = "HEAD"
clone := r.clone(c)
clone.handle(handlers)
return clone
}
// Post handles POST methods with a given pattern
func (r *Router) Post(pattern string, handlers ...interface{}) *Router {
c := r.pattern(pattern)
c.Method = "POST"
clone := r.clone(c)
clone.handle(handlers)
return clone
}
// Put handles PUT methods with a given pattern
func (r *Router) Put(pattern string, handlers ...interface{}) *Router {
c := r.pattern(pattern)
c.Method = "PUT"
clone := r.clone(c)
clone.handle(handlers)
return clone
}
// Delete handles DELETE methods with a given pattern
func (r *Router) Delete(pattern string, handlers ...interface{}) *Router {
c := r.pattern(pattern)
c.Method = "DELETE"
clone := r.clone(c)
clone.handle(handlers)
return clone
}
// Use attaches middleware to a route
func (r *Router) Use(middleware ...interface{}) {
r.dispatcher.Middleware(r.route, r.buildMiddlewares(middleware)...)
}
// NotFound specifys a not found handler for a route
func (r *Router) NotFound(i interface{}) {
handler := r.buildHandler(i)
r.dispatcher.NotFoundHandler(r.route, handler)
}
func (r *Router) clone(route Route) *Router {
return &Router{
dispatcher: r.dispatcher,
route: route,
builders: r.builders,
}
}
// AddHandlerBuilder adds a builder to construct handlers
func (r *Router) AddHandlerBuilder(builder func(interface{}) (Handler, error)) {
r.builders = append(r.builders, &builder)
}
func (r *Router) handle(i []interface{}) {
handlers := r.buildHandlers(i)
r.dispatcher.Handle(r.route, handlers...)
}
func (r *Router) buildMiddleware(i interface{}) Middleware {
switch i.(type) {
case Middleware:
return i.(Middleware)
case func(Handler) Handler:
return Middleware(i.(func(Handler) Handler))
case func(http.Handler) http.Handler:
h := i.(func(http.Handler) http.Handler)
return Middleware(func(next Handler) Handler {
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
n := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.CtxServeHTTP(ctx, w, r)
})
h(n).ServeHTTP(w, r)
})
})
}
panic("Invalid middleware passsed to router")
}
func (r *Router) buildMiddlewares(i []interface{}) []Middleware {
middleware := make([]Middleware, len(i))
for j, h := range i {
middleware[j] = r.buildMiddleware(h)
}
return middleware
}
func (r *Router) buildHandler(i interface{}) Handler {
switch i.(type) {
case Handler:
return i.(Handler)
case func(context.Context, http.ResponseWriter, *http.Request):
return HandlerFunc(i.(func(context.Context, http.ResponseWriter, *http.Request)))
case http.Handler:
h := i.(http.Handler)
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
})
case func(http.ResponseWriter, *http.Request):
h := i.(func(http.ResponseWriter, *http.Request))
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
h(w, r)
})
}
for _, b := range r.builders {
h, err := (*b)(i)
if err == nil {
return h
}
}
panic("Invalid handler passsed to router")
}
func (r *Router) buildHandlers(i []interface{}) []Handler {
handlers := make([]Handler, len(i))
for j, h := range i {
handlers[j] = r.buildHandler(h)
}
return handlers
}
func (r *Router) pattern(pattern string) Route {
c := r.route.clone()
c.Pattern = path.Join(r.route.Pattern, pattern)
return c
}