/
handler.go
261 lines (229 loc) · 7.54 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
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
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
package lambda
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/aws/aws-lambda-go/lambda/handlertrace"
)
type Handler interface {
Invoke(ctx context.Context, payload []byte) ([]byte, error)
}
type handlerOptions struct {
Handler
baseContext context.Context
jsonResponseEscapeHTML bool
jsonResponseIndentPrefix string
jsonResponseIndentValue string
enableSIGTERM bool
sigtermCallbacks []func()
}
type Option func(*handlerOptions)
// WithContext is a HandlerOption that sets the base context for all invocations of the handler.
//
// Usage:
// lambda.StartWithOptions(
// func (ctx context.Context) (string, error) {
// return ctx.Value("foo"), nil
// },
// lambda.WithContext(context.WithValue(context.Background(), "foo", "bar"))
// )
func WithContext(ctx context.Context) Option {
return Option(func(h *handlerOptions) {
h.baseContext = ctx
})
}
// WithSetEscapeHTML sets the SetEscapeHTML argument on the underlying json encoder
//
// Usage:
// lambda.StartWithOptions(
// func () (string, error) {
// return "<html><body>hello!></body></html>", nil
// },
// lambda.WithSetEscapeHTML(true),
// )
func WithSetEscapeHTML(escapeHTML bool) Option {
return Option(func(h *handlerOptions) {
h.jsonResponseEscapeHTML = escapeHTML
})
}
// WithSetIndent sets the SetIndent argument on the underling json encoder
//
// Usage:
// lambda.StartWithOptions(
// func (event any) (any, error) {
// return event, nil
// },
// lambda.WithSetIndent(">"," "),
// )
func WithSetIndent(prefix, indent string) Option {
return Option(func(h *handlerOptions) {
h.jsonResponseIndentPrefix = prefix
h.jsonResponseIndentValue = indent
})
}
// WithEnableSIGTERM enables SIGTERM behavior within the Lambda platform on container spindown.
// SIGKILL will occur ~500ms after SIGTERM.
// Optionally, an array of callback functions to run on SIGTERM may be provided.
//
// Usage:
// lambda.StartWithOptions(
// func (event any) (any, error) {
// return event, nil
// },
// lambda.WithEnableSIGTERM(func() {
// log.Print("function container shutting down...")
// })
// )
func WithEnableSIGTERM(callbacks ...func()) Option {
return Option(func(h *handlerOptions) {
h.sigtermCallbacks = append(h.sigtermCallbacks, callbacks...)
h.enableSIGTERM = true
})
}
func validateArguments(handler reflect.Type) (bool, error) {
handlerTakesContext := false
if handler.NumIn() > 2 {
return false, fmt.Errorf("handlers may not take more than two arguments, but handler takes %d", handler.NumIn())
} else if handler.NumIn() > 0 {
contextType := reflect.TypeOf((*context.Context)(nil)).Elem()
argumentType := handler.In(0)
handlerTakesContext = argumentType.Implements(contextType)
if handler.NumIn() > 1 && !handlerTakesContext {
return false, fmt.Errorf("handler takes two arguments, but the first is not Context. got %s", argumentType.Kind())
}
}
return handlerTakesContext, nil
}
func validateReturns(handler reflect.Type) error {
errorType := reflect.TypeOf((*error)(nil)).Elem()
switch n := handler.NumOut(); {
case n > 2:
return fmt.Errorf("handler may not return more than two values")
case n > 1:
if !handler.Out(1).Implements(errorType) {
return fmt.Errorf("handler returns two values, but the second does not implement error")
}
case n == 1:
if !handler.Out(0).Implements(errorType) {
return fmt.Errorf("handler returns a single value, but it does not implement error")
}
}
return nil
}
// NewHandler creates a base lambda handler from the given handler function. The
// returned Handler performs JSON serialization and deserialization, and
// delegates to the input handler function. The handler function parameter must
// satisfy the rules documented by Start. If handlerFunc is not a valid
// handler, the returned Handler simply reports the validation error.
func NewHandler(handlerFunc interface{}) Handler {
return NewHandlerWithOptions(handlerFunc)
}
// NewHandlerWithOptions creates a base lambda handler from the given handler function. The
// returned Handler performs JSON serialization and deserialization, and
// delegates to the input handler function. The handler function parameter must
// satisfy the rules documented by Start. If handlerFunc is not a valid
// handler, the returned Handler simply reports the validation error.
func NewHandlerWithOptions(handlerFunc interface{}, options ...Option) Handler {
return newHandler(handlerFunc, options...)
}
func newHandler(handlerFunc interface{}, options ...Option) *handlerOptions {
if h, ok := handlerFunc.(*handlerOptions); ok {
return h
}
h := &handlerOptions{
baseContext: context.Background(),
jsonResponseEscapeHTML: false,
jsonResponseIndentPrefix: "",
jsonResponseIndentValue: "",
}
for _, option := range options {
option(h)
}
if h.enableSIGTERM {
enableSIGTERM(h.sigtermCallbacks)
}
h.Handler = reflectHandler(handlerFunc, h)
return h
}
type bytesHandlerFunc func(context.Context, []byte) ([]byte, error)
func (h bytesHandlerFunc) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
return h(ctx, payload)
}
func errorHandler(err error) Handler {
return bytesHandlerFunc(func(_ context.Context, _ []byte) ([]byte, error) {
return nil, err
})
}
func reflectHandler(handlerFunc interface{}, h *handlerOptions) Handler {
if handlerFunc == nil {
return errorHandler(errors.New("handler is nil"))
}
if handler, ok := handlerFunc.(Handler); ok {
return handler
}
handler := reflect.ValueOf(handlerFunc)
handlerType := reflect.TypeOf(handlerFunc)
if handlerType.Kind() != reflect.Func {
return errorHandler(fmt.Errorf("handler kind %s is not %s", handlerType.Kind(), reflect.Func))
}
takesContext, err := validateArguments(handlerType)
if err != nil {
return errorHandler(err)
}
if err := validateReturns(handlerType); err != nil {
return errorHandler(err)
}
return bytesHandlerFunc(func(ctx context.Context, payload []byte) ([]byte, error) {
in := bytes.NewBuffer(payload)
out := bytes.NewBuffer(nil)
decoder := json.NewDecoder(in)
encoder := json.NewEncoder(out)
encoder.SetEscapeHTML(h.jsonResponseEscapeHTML)
encoder.SetIndent(h.jsonResponseIndentPrefix, h.jsonResponseIndentValue)
trace := handlertrace.FromContext(ctx)
// construct arguments
var args []reflect.Value
if takesContext {
args = append(args, reflect.ValueOf(ctx))
}
if (handlerType.NumIn() == 1 && !takesContext) || handlerType.NumIn() == 2 {
eventType := handlerType.In(handlerType.NumIn() - 1)
event := reflect.New(eventType)
if err := decoder.Decode(event.Interface()); err != nil {
return nil, err
}
if nil != trace.RequestEvent {
trace.RequestEvent(ctx, event.Elem().Interface())
}
args = append(args, event.Elem())
}
response := handler.Call(args)
// return the error, if any
if len(response) > 0 {
if errVal, ok := response[len(response)-1].Interface().(error); ok && errVal != nil {
return nil, errVal
}
}
// set the response value, if any
var val interface{}
if len(response) > 1 {
val = response[0].Interface()
if nil != trace.ResponseEvent {
trace.ResponseEvent(ctx, val)
}
}
if err := encoder.Encode(val); err != nil {
return nil, err
}
responseBytes := out.Bytes()
// back-compat, strip the encoder's trailing newline unless WithSetIndent was used
if h.jsonResponseIndentValue == "" && h.jsonResponseIndentPrefix == "" {
return responseBytes[:len(responseBytes)-1], nil
}
return responseBytes, nil
})
}