-
Notifications
You must be signed in to change notification settings - Fork 0
/
caller.go
331 lines (295 loc) · 7.43 KB
/
caller.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
package mantil
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"runtime"
"strconv"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/mantil-io/mantil.go/proto"
"github.com/mitchellh/mapstructure"
)
const (
// ApiErrorHeader is the response header key for lambda errors
ApiErrorHeader = "x-api-error"
// ApiErrorCodeHeader is the response header key for lambda error codes
ApiErrorCodeHeader = "x-api-error-code"
)
func newCaller(i interface{}) *caller {
return &caller{
value: reflect.ValueOf(i),
typ: reflect.TypeOf(i),
}
}
type caller struct {
value reflect.Value
typ reflect.Type
}
type response struct {
value interface{}
payload []byte
err error
statusCode int
}
type iStatusCode interface {
StatusCode() int
}
type iErrorCode interface {
ErrorCode() int
}
func (c *response) StatusCode() int {
if c.err != nil {
if isc, ok := c.err.(iStatusCode); ok {
return isc.StatusCode()
}
if c.statusCode != 0 {
return c.statusCode
}
return http.StatusInternalServerError
}
if c.statusCode != 0 {
return c.statusCode
}
return http.StatusOK
}
func (c *response) ErrorCode() int {
if c.err != nil {
if iec, ok := c.err.(iErrorCode); ok {
return iec.ErrorCode()
}
}
return 0
}
func (c *response) Error() string {
if c.err == nil {
return ""
}
return c.err.Error()
}
func (c *response) Err() error {
return c.err
}
func (c *response) Value() interface{} {
return c.value
}
func (c *response) Body() string {
if c.payload == nil {
return ""
}
return string(c.payload)
}
func (c *response) Raw() ([]byte, error) {
return c.payload, c.err
}
func (c *response) AsAPIGateway() ([]byte, error) {
var gwRsp events.APIGatewayProxyResponse
gwRsp.StatusCode = c.StatusCode()
body := c.Body()
gwRsp.Body = body
hdrs := make(map[string]string)
if e := c.Error(); e != "" {
hdrs[ApiErrorHeader] = e
}
if ec := c.ErrorCode(); ec != 0 {
hdrs[ApiErrorCodeHeader] = strconv.Itoa(ec)
}
// try to set right content types
if len(body) > 1 && (strings.HasPrefix(body, "{") || strings.HasPrefix(body, "[")) {
hdrs["Content-Type"] = "application/json"
}
gwRsp.Headers = hdrs
return json.Marshal(gwRsp)
}
func (c *response) AsWS() ([]byte, error) {
if c.err != nil {
return nil, c.err
}
gwRsp := events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
}
return json.Marshal(gwRsp)
}
func (c *response) AsStreaming(req Request) (*proto.Message, error) {
if c.err != nil {
return nil, c.err
}
rm := req.toStreamingResponse(c.payload)
return &rm, nil
}
func okEmptyResponse() response {
return okResponse(nil, nil)
}
func okResponse(payload []byte, val interface{}) response {
if payload == nil {
return response{
statusCode: http.StatusNoContent,
}
}
return response{
value: val,
payload: payload,
statusCode: http.StatusOK,
}
}
func errResponse(err error, statusCode int) response {
if statusCode == 0 {
statusCode = http.StatusInternalServerError
}
return response{
statusCode: statusCode,
err: err,
}
}
// Inspiration: https://github.com/aws/aws-lambda-go/blob/master/lambda/handler.go
func (c *caller) call(ctx context.Context, reqPayload []byte, reqParams map[string]string, methodNames ...string) response {
for _, methodName := range methodNames {
methodName = strings.Replace(strings.ToLower(methodName), "-", "", -1)
if methodName == "" {
for _, name := range []string{"Invoke", "Root", "Default"} {
if method, ok := c.typ.MethodByName(name); ok {
return c.callMethod(ctx, method, reqPayload, reqParams)
}
}
return errResponse(
fmt.Errorf("can't find Invoke/Root/Default method in %s", c.typ.Name()),
http.StatusNotImplemented,
)
}
for i := 0; i < c.typ.NumMethod(); i++ {
method := c.typ.Method(i)
if methodName != strings.ToLower(method.Name) {
continue
}
return c.callMethod(ctx, method, reqPayload, reqParams)
}
}
return errResponse(
fmt.Errorf("method %v not found", methodNames),
http.StatusNotImplemented,
)
}
func (c *caller) callMethod(ctx context.Context, method reflect.Method, reqPayload []byte, reqParams map[string]string) response {
args, cr := c.args(ctx, method, reqPayload, reqParams)
if cr != nil {
return *cr
}
rspArgs, cr := c.callWithRecover(method.Func, args)
if cr != nil {
return *cr
}
return c.parseRspArgs(rspArgs)
}
func (c *caller) callWithRecover(fun reflect.Value, args []reflect.Value) (rpsArgs []reflect.Value, cr *response) {
defer func() {
if r := recover(); r != nil {
// log panic stack trace
stackTrace := make([]byte, 8192)
_ = runtime.Stack(stackTrace, false)
if logPanic {
info("PANIC %s, stack: %s", r, stackTrace)
}
cErr := errResponse(fmt.Errorf("PANIC %s", r), http.StatusInternalServerError)
cr = &cErr
}
}()
cr = nil
rpsArgs = fun.Call(args)
return
}
func (c *caller) args(ctx context.Context, method reflect.Method, reqPayload []byte, reqParams map[string]string) ([]reflect.Value, *response) {
numIn := method.Type.NumIn()
methodTakesContext := false
if numIn > 1 {
contextType := reflect.TypeOf((*context.Context)(nil)).Elem()
argumentType := method.Type.In(1)
methodTakesContext = argumentType.Implements(contextType)
}
// construct arguments
var args []reflect.Value
args = append(args, c.value)
if methodTakesContext {
args = append(args, reflect.ValueOf(ctx))
}
if numIn == len(args)+1 {
// unmarshal reqPayload into type
eventType := method.Type.In(numIn - 1)
event := reflect.New(eventType)
// query params from GET request
if len(reqParams) > 0 {
if err := mapstructure.WeakDecode(reqParams, event.Interface()); err != nil {
cr := errResponse(
fmt.Errorf("unable to unmarshal request into %s, error: %w", eventType.Name(), err),
http.StatusBadRequest)
return nil, &cr
}
args = append(args, event.Elem())
return args, nil
}
// process payload
if len(reqPayload) == 0 && eventType.Kind() == reflect.Ptr {
args = append(args, event.Elem())
return args, nil
}
switch eventType.Kind() {
case reflect.String:
args = append(args, reflect.ValueOf(string(reqPayload)))
default:
if len(reqPayload) == 0 {
reqPayload = []byte(`{}`)
}
if err := json.Unmarshal(reqPayload, event.Interface()); err != nil {
cr := errResponse(
fmt.Errorf("unable to unmarshal request into %s, error: %w", eventType.Name(), err),
http.StatusBadRequest,
)
return nil, &cr
}
args = append(args, event.Elem())
}
}
return args, nil
}
func (c *caller) parseRspArgs(args []reflect.Value) response {
if len(args) == 0 {
return okEmptyResponse()
}
// convert return values into (interface{}, error)
var err error
var isLastArgError bool
if len(args) > 0 {
if errVal, ok := args[len(args)-1].Interface().(error); ok {
err = errVal
isLastArgError = true
}
}
if err != nil {
return errResponse(err, http.StatusInternalServerError)
}
if len(args) == 1 && isLastArgError {
return okEmptyResponse()
}
val := args[0].Interface()
if val == nil {
return okEmptyResponse()
}
var rspPayload []byte
switch v := val.(type) {
case []byte:
rspPayload = v
case string:
rspPayload = []byte(v)
default:
// marshal val
rspPayload, err = json.Marshal(val)
if err != nil {
return errResponse(fmt.Errorf("unable to marshal response, error %w", err), http.StatusInternalServerError)
}
if len(rspPayload) == 4 && string(rspPayload) == "null" {
return okEmptyResponse()
}
}
return okResponse(rspPayload, val)
}