-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec.go
59 lines (50 loc) · 1.39 KB
/
codec.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
package ep
import (
"net/http"
"reflect"
"github.com/advanderveer/ep/epcoding"
)
// Codec provides http.Handlers that automatically decode requests and encode
// responses based on input and output structs
type Codec struct {
resHooks []ResponseHook
reqHooks []RequestHook
errHooks []ErrorHook
decodings []epcoding.Decoding
encodings []epcoding.Encoding
}
// New initiates a new ep Codec
func New(opts ...Option) (c *Codec) {
c = &Codec{}
Options(opts...).apply(c)
return
}
// Handle will initiate an http handler that handles request according to
// the Codec configuration.
func (c *Codec) Handle(f interface{}) http.Handler {
switch ft := f.(type) {
case func(ResponseWriter, *http.Request):
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := NewResponse(w, r, c.reqHooks, c.resHooks, c.errHooks, c.decodings, c.encodings)
defer res.Recover()
ft(res, r)
})
default:
clb, err := newCallable(f)
if err != nil {
panic("ep: failed to turn argument into a handler: " + err.Error())
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := NewResponse(w, r, c.reqHooks, c.resHooks, c.errHooks, c.decodings, c.encodings)
defer res.Recover()
ok := true
inv := clb.Input()
if (inv != reflect.Value{}) {
ok = res.Bind(inv.Interface())
}
if ok {
res.Render(clb.Call(clb.Args(r, inv))...)
}
})
}
}