-
Notifications
You must be signed in to change notification settings - Fork 1
/
invoke.go
52 lines (45 loc) · 1005 Bytes
/
invoke.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
package ridgenative
import (
"context"
"encoding/json"
"io"
"net/http"
)
type invoke struct {
id string
payload []byte
headers http.Header
}
func callBytesHandlerFunc(ctx context.Context, payload []byte, h handlerFunc) (response []byte, err error) {
defer func() {
if v := recover(); v != nil {
err = lambdaPanicResponse(v)
}
}()
var req *request
if err := json.Unmarshal(payload, &req); err != nil {
return nil, err
}
resp, err := h(ctx, req)
if err != nil {
return nil, err
}
return json.Marshal(resp)
}
func callHandlerFuncSteaming(ctx context.Context, payload []byte, h handlerFuncSteaming) (response io.ReadCloser, contentType string, err error) {
defer func() {
if v := recover(); v != nil {
err = lambdaPanicResponse(v)
}
}()
var req *request
if err := json.Unmarshal(payload, &req); err != nil {
return nil, "", err
}
r, w := io.Pipe()
contentType, err = h(ctx, req, w)
if err != nil {
return nil, "", err
}
return r, contentType, nil
}