This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
446 lines (365 loc) · 10.1 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* This file is part of the Model Rocket Hiro Stack
* Copyright (c) 2020 Model Rocket LLC.
*
* https://github.com/ModelRocket/hiro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more detailr.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package api
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"path"
"reflect"
"runtime/debug"
"strings"
"github.com/ModelRocket/hiro/pkg/api/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/mr-tron/base58"
)
type (
// Router is an api Router
Router struct {
*Server
Mux *mux.Router
basePath string
version string
versioning bool
name string
context func(context.Context) interface{}
authorizers []Authorizer
hooks []RouteHook
sessions *session.Manager
}
// RouteHook methods are called before the handler to allow for added context
RouteHook func(r *http.Request, rt Route) error
// RouterOption are router options
RouterOption func(r *Router)
)
// AddRoutes adds a routes to the router
func (r *Router) AddRoutes(routes ...Route) *Router {
for _, rt := range routes {
fn := reflect.ValueOf(rt)
if fn.Kind() != reflect.Func {
panic(fmt.Errorf("route %s is not a function", rt.Name()))
}
r.Mux.Name(rt.Name()).
Methods(rt.Methods()...).
Path(rt.Path()).
HandlerFunc(r.handler(rt))
}
return r
}
// WithVersioning enables versioning that will enforce a versioned path
func WithVersioning(version ...string) RouterOption {
return func(r *Router) {
if len(version) > 0 {
r.version = version[0]
}
r.versioning = true
r.basePath = path.Join(r.basePath, "{version}")
r.Mux = r.router.PathPrefix(r.basePath).Subrouter()
}
}
// WithName enables versioning that will enforce a versioned path
func (r *Router) WithName(name string) RouterOption {
return func(r *Router) {
r.name = name
}
}
// WithSessionManager adds the session store to the router
func WithSessionManager(store *session.Manager) RouterOption {
return func(r *Router) {
r.sessions = store
}
}
// WithContext adds context to the router
func WithContext(ctx func(context.Context) interface{}) RouterOption {
return func(r *Router) {
r.context = ctx
}
}
// WithHooks sets the hooks for the router
func WithHooks(hooks ...RouteHook) RouterOption {
return func(r *Router) {
r.hooks = hooks
}
}
// WithAuthorizers sets the authorizers for the router
func WithAuthorizers(auth ...Authorizer) RouterOption {
return func(r *Router) {
r.authorizers = auth
}
}
// Version implements the Versioner interface
func (r Router) Version() string {
return r.version
}
// Name implements the Versioner interface
func (r Router) Name() string {
return r.name
}
// RequireVersion implements the Versioner interface
func (r Router) RequireVersion() bool {
return r.versioning
}
func (r *Router) handler(rt Route) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
var resp interface{}
var cache bool
// add the log to the context
id := uuid.Must(uuid.NewUUID())
reqID := base58.Encode(id[:])
log := r.log.
WithField("req-id", reqID).
WithField("route", rt.Name())
*req = *req.WithContext(
context.WithValue(
req.Context(),
contextKeyLogger,
log))
trace := r.tracingEnabled
// disable caching if the header says so or its disabled via 0 ttl
if req.Header.Get("Cache-Control") == "no-cache" || r.cache == nil {
cache = false
}
// add the request object to the context
rc := &requestContext{req, w}
req = req.WithContext(context.WithValue(req.Context(), contextKeyRequest, rc))
defer func() {
if err := recover(); err != nil {
if _, ok := err.(Responder); !ok {
debug.PrintStack()
if e, ok := err.(error); ok {
r.WriteError(w, http.StatusInternalServerError, e)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
return
}
// ensure the error is returned properly
resp = err
}
switch t := resp.(type) {
case Responder:
if cache || trace {
rec := httptest.NewRecorder()
if err := t.Write(rec); err != nil {
log.Error(err.Error())
r.WriteError(w, http.StatusInternalServerError, err)
return
}
dump, err := httputil.DumpResponse(rec.Result(), cache || rec.Body.Len() < 1024)
if err != nil {
log.Error(err.Error())
r.WriteError(w, http.StatusInternalServerError, err)
return
}
if trace {
log.Debugf("%s <- %s", req.RequestURI, (dump))
}
if cr, ok := rt.(CachedRoute); ok && cache {
r.cache.Set(req.RequestURI, dump, cr.Timeout())
}
for k, vals := range rec.Header() {
for _, v := range vals {
w.Header().Add(k, v)
}
}
w.WriteHeader(rec.Code)
w.Write(rec.Body.Bytes())
return
}
if err := t.Write(w); err != nil {
panic(err)
}
case *http.Response:
t.Header.Write(w)
t.Write(w)
case error:
r.WriteError(w, http.StatusInternalServerError, t)
}
}()
if r.context != nil {
req = req.WithContext(context.WithValue(req.Context(), contextKeyContext, r.context(req.Context())))
}
if r.sessions != nil {
req = req.WithContext(context.WithValue(req.Context(), contextKeySessions, r.sessions))
}
if at, ok := rt.(AuthorizedRoute); ok {
prins := make([]Principal, 0)
for _, a := range r.authorizers {
for _, ct := range at.RequireAuth() {
if ct != a.CredentialType() {
continue
}
ctx, err := a.Authorize(req, rt)
if err != nil && !errors.Is(err, ErrAuthUnacceptable) {
if re, ok := err.(Responder); ok {
resp = re
} else {
panic(err)
}
return
}
if ctx != nil {
// append this to the principal chain
prins = append(prins, ctx)
}
break
}
}
if len(prins) == 0 {
panic(ErrUnauthorized)
}
req = req.WithContext(context.WithValue(req.Context(), contextKeyAuth, prins))
}
if _, ok := rt.(CachedRoute); ok && cache {
if val, ok := r.cache.Get(req.RequestURI); ok {
var err error
resp, err = http.ReadResponse(bufio.NewReader(bytes.NewReader(val.([]byte))), req)
if err != nil {
resp = nil
panic(err)
}
}
}
w.Header().Set("X-Hiro-Request-ID", reqID)
rc.r = req
// Add any additional context from the caller
for _, h := range r.hooks {
if err := h(req, rt); err != nil {
panic(err)
}
}
req = req.WithContext(context.WithValue(req.Context(), contextKeyRequest, rc))
rc.r = req
fn := reflect.ValueOf(rt)
// check for standard HandlerFunc or http.HandlerFunc handlers
if h, ok := fn.Interface().(HandlerFunc); ok {
resp = h(w, req)
return
} else if h, ok := fn.Interface().(http.HandlerFunc); ok {
h(w, req)
return
}
args := []reflect.Value{}
if fn.Type().In(0) != reflect.TypeOf((*context.Context)(nil)).Elem() {
panic(fmt.Errorf("first argument of handler must be context.Context"))
}
args = append(args, reflect.ValueOf(req.Context()))
if fn.Type().NumIn() > 1 {
pt := fn.Type().In(1)
if pt.Kind() != reflect.Ptr {
panic(ErrServerError.
WithDetail(
fmt.Sprintf("route %s %s parameter %s but be a pointer", req.Method, rt.Path(), pt.Name())))
}
pt = pt.Elem()
params := reflect.New(pt).Interface()
decoder := schema.NewDecoder()
decoder.SetAliasTag("json")
decoder.IgnoreUnknownKeys(true)
decoder.RegisterConverter([]string{}, func(input string) reflect.Value {
if strings.Contains(input, ",") {
return reflect.ValueOf(strings.Split(input, ","))
}
return reflect.ValueOf(strings.Fields(input))
})
vars := mux.Vars(req)
if len(vars) > 0 {
vals := make(url.Values)
for k, v := range vars {
vals.Add(k, v)
}
if err := decoder.Decode(params, vals); err != nil {
panic(err)
}
}
if len(req.URL.Query()) > 0 {
if err := decoder.Decode(params, req.URL.Query()); err != nil {
panic(err)
}
}
if req.Body != nil && req.ContentLength > 0 {
t, _, err := mime.ParseMediaType(req.Header.Get("Content-type"))
if err != nil {
panic(err)
}
switch t {
case "application/json":
data, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err)
}
if err := json.Unmarshal(data, params); err != nil {
panic(err)
}
req.Body = ioutil.NopCloser(bytes.NewReader(data))
case "application/x-www-form-urlencoded":
if err := req.ParseForm(); err != nil {
panic(err)
}
if err := decoder.Decode(params, req.Form); err != nil {
panic(err)
}
case "multipart/form-data":
if err := req.ParseMultipartForm(1024 * 1024 * 128); err != nil {
panic(err)
}
if err := decoder.Decode(params, req.Form); err != nil {
panic(err)
}
}
}
if v, ok := params.(validation.Validatable); ok {
if err := v.Validate(); err != nil {
panic(ErrBadRequest.WithError(err))
}
} else if v, ok := params.(validation.ValidatableWithContext); ok {
if err := v.ValidateWithContext(req.Context()); err != nil {
panic(ErrBadRequest.WithError(err))
}
}
args = append(args, reflect.ValueOf(params))
}
if r.tracingEnabled {
if dump, err := httputil.DumpRequest(req, true); err == nil {
log.Debugf("%s -> %s", req.RequestURI, (dump))
}
}
rc.r = req
rval := fn.Call(args)
if len(rval) > 0 {
resp = rval[0].Interface()
}
}
}
// ServeHTTP implements the http.Handler interface
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.Mux.ServeHTTP(w, req)
}