-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
51 lines (42 loc) · 1.05 KB
/
handler.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
package delta
import (
"context"
"fmt"
"net/http"
"github.com/aws/aws-lambda-go/lambda"
)
type Handler struct {
c *config
h http.Handler
}
// make sure it implements lambda's & http's handler
var _ lambda.Handler = &Handler{}
var _ http.Handler = &Handler{}
func NewHandler(h http.Handler, opts ...Options) *Handler {
var c config
for _, o := range opts {
o(&c)
}
if c.transformer == nil {
c.transformer = GetDefaultTransformer()
}
return &Handler{h: h, c: &c}
}
// Invoke implements lambda.Handler
func (lh *Handler) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
req, err := lh.c.transformer.Request(ctx, payload)
if err != nil {
return nil, fmt.Errorf("can not transform request payload: %w", err)
}
res := NewResponseWriter()
res.encode = lh.c.encodeResponse
lh.h.ServeHTTP(res, req)
b, err := lh.c.transformer.Response(ctx, res)
if err != nil {
return nil, fmt.Errorf("can not transform response: %w", err)
}
return b, nil
}
func (lh *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
lh.h.ServeHTTP(w, r)
}