-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
183 lines (155 loc) · 4.77 KB
/
engine.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
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
/**
* mostly taken from aws lambda go stub for
* best compatibility. This will have to change
* to allow usage of WAS protocol and
*/
package functions
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/fcgi"
"reflect"
)
type Handler interface {
Invoke(ctx context.Context, payload []byte) ([]byte, error)
}
type server struct {
Handler
baseContext context.Context
jsonResponseEscapeHTML bool
jsonResponseIndentPrefix string
jsonResponseIndentValue string
}
func (s server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "can't read body", http.StatusBadRequest)
return
}
data, err := s.Invoke(s.baseContext, body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(data)
}
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 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
}
func reflectHandler(handlerFunc interface{}, s *server) 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(s.jsonResponseEscapeHTML)
encoder.SetIndent(s.jsonResponseIndentPrefix, s.jsonResponseIndentValue)
// 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
}
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 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 s.jsonResponseIndentValue == "" && s.jsonResponseIndentPrefix == "" {
return responseBytes[:len(responseBytes)-1], nil
}
return responseBytes, nil
})
}
func newServer(handlerFunc interface{}) *server {
s := &server{
baseContext: context.Background(),
jsonResponseEscapeHTML: false,
jsonResponseIndentPrefix: "",
jsonResponseIndentValue: "",
}
s.Handler = reflectHandler(handlerFunc, s)
return s
}
func Start(handler interface{}) {
fcgi.Serve(nil, newServer(handler))
}