forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
80 lines (74 loc) · 2.4 KB
/
middleware.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
package goa
import (
"fmt"
"net/http"
"context"
)
type (
// Middleware represents the canonical goa middleware signature.
Middleware func(Handler) Handler
)
// NewMiddleware creates a middleware from the given argument. The allowed types for the
// argument are:
//
// - a goa middleware: goa.Middleware or func(goa.Handler) goa.Handler
//
// - a goa handler: goa.Handler or func(context.Context, http.ResponseWriter, *http.Request) error
//
// - an http middleware: func(http.Handler) http.Handler
//
// - or an http handler: http.Handler or func(http.ResponseWriter, *http.Request)
//
// An error is returned if the given argument is not one of the types above.
func NewMiddleware(m interface{}) (mw Middleware, err error) {
switch m := m.(type) {
case Middleware:
mw = m
case func(Handler) Handler:
mw = m
case Handler:
mw = handlerToMiddleware(m)
case func(context.Context, http.ResponseWriter, *http.Request) error:
mw = handlerToMiddleware(m)
case func(http.Handler) http.Handler:
mw = func(h Handler) Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = h(ctx, w, r)
})).ServeHTTP(rw, req)
return
}
}
case http.Handler:
mw = httpHandlerToMiddleware(m.ServeHTTP)
case func(http.ResponseWriter, *http.Request):
mw = httpHandlerToMiddleware(m)
default:
err = fmt.Errorf("invalid middleware %#v", m)
}
return
}
// handlerToMiddleware creates a middleware from a raw handler.
// The middleware calls the handler and either breaks the middleware chain if the handler returns
// an error by also returning the error or calls the next handler in the chain otherwise.
func handlerToMiddleware(m Handler) Middleware {
return func(h Handler) Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
if err := m(ctx, rw, req); err != nil {
return err
}
return h(ctx, rw, req)
}
}
}
// httpHandlerToMiddleware creates a middleware from a http.HandlerFunc.
// The middleware calls the ServerHTTP method exposed by the http handler and then calls the next
// middleware in the chain.
func httpHandlerToMiddleware(m http.HandlerFunc) Middleware {
return func(h Handler) Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
m.ServeHTTP(rw, req)
return h(ctx, rw, req)
}
}
}