-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner_http.go
292 lines (253 loc) · 7.75 KB
/
runner_http.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
package fdk
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
const (
envPort = "PORT"
mb = 1 << 20
)
func runHTTP(ctx context.Context, newHandlerFn func(context.Context, *slog.Logger) Handler) {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true}))
handler := newHandlerFn(ctx, logger)
mux := http.NewServeMux()
mux.Handle("/", dispatchReq(logger, handler))
s := &http.Server{
Addr: fmt.Sprintf(":%d", port()),
Handler: mux,
MaxHeaderBytes: mb,
}
go func() {
select {
case <-ctx.Done():
shutdownCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
logger.Info("shutting down HTTP server...")
if err := s.Shutdown(shutdownCtx); err != nil {
logger.Error("failed to shutdown server", "err", err)
}
}
}()
logger.Info("serving HTTP server on port " + strconv.Itoa(port()))
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error("unexpected shutdown of server", "err", err)
}
}
func dispatchReq(logger *slog.Logger, handler Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
defer func() {
if n, err := io.Copy(io.Discard, req.Body); err != nil {
logger.Error("failed to drain request body", "err", err.Error(), "bytes_drained", n)
}
if err := req.Body.Close(); err != nil {
logger.Error("failed to close request body", "err", err.Error())
}
}()
r, closeFn, err := toRequest(req)
if err != nil {
logger.Error("failed to create request", "err", err)
writeErr := writeResponse(logger, w, ErrResp(APIError{Code: http.StatusInternalServerError, Message: "unable to process incoming request"}))
if writeErr != nil {
logger.Error("failed to write failed request response", "err", writeErr)
}
return
}
defer func() {
if err := closeFn(); err != nil {
logger.Error("failed to close request body", "err", err.Error())
}
}()
const ctxKeyTraceID = "_traceid"
ctx := context.WithValue(req.Context(), ctxKeyTraceID, r.TraceID)
resp := handler.Handle(ctx, r)
if f, ok := resp.Body.(File); ok {
f = NormalizeFile(f)
sha256Hash, size, err := writeFile(logger, f.Contents, f.Filename)
if err != nil {
resp.Errors = append(resp.Errors, APIError{Code: http.StatusInternalServerError, Message: err.Error()})
writeErr := writeResponse(logger, w, resp)
if writeErr != nil {
logger.Error("failed to write failed request response", "write_err", writeErr, "err", err.Error())
}
return
}
// the sha and size will be left to the runner to determine. This removes the chicken and egg
// problem where you need hte size and sha but want to work with the stream only. This isn't
// possible without having the runner do it. We can maintain streaming semantics while also
// obtaining our sha/size by extending the writer to support this when we're moving the
// contents to disk (or w/e sync we use)
respBody := struct {
ContentType string `json:"content_type"`
Encoding string `json:"encoding"`
Filename string `json:"filename"`
SHA256 string `json:"sha256_checksum"`
Size int `json:"size,string"`
}{
ContentType: f.ContentType,
Encoding: f.Encoding,
Filename: f.Filename,
SHA256: sha256Hash,
Size: size,
}
resp.Body = JSON(respBody)
}
err = writeResponse(logger, w, resp)
if err != nil {
logger.Error("failed to write response", "err", err)
}
})
}
func toRequest(req *http.Request) (Request, func() error, error) {
fromFn := fromJSONReq
if strings.HasPrefix(req.Header.Get("Content-Type"), "multipart/form-data") {
fromFn = fromMultipartReq
}
r, body, err := fromFn(req)
if err != nil {
return Request{}, nil, fmt.Errorf("failed to prepare request: %w", err)
}
// Ensure headers are canonically formatted else header.Get("my-key") won't necessarily work.
hCanon := make(http.Header)
for k, v := range r.Headers {
for _, s := range v {
hCanon.Add(k, s)
}
}
r.Headers = hCanon
return reqMetaToRequest(r, body), body.Close, nil
}
type reqMeta struct {
FnID string `json:"fn_id"`
FnVersion int `json:"fn_version"`
Context json.RawMessage `json:"context"`
AccessToken string `json:"access_token"`
Method string `json:"method"`
Headers http.Header `json:"header"`
Queries url.Values `json:"query"`
URL string `json:"url"`
TraceID string `json:"trace_id"`
}
func reqMetaToRequest(r reqMeta, body io.Reader) Request {
return Request{
FnID: r.FnID,
FnVersion: r.FnVersion,
Body: body,
Context: r.Context,
Headers: r.Headers,
Queries: r.Queries,
Method: r.Method,
URL: r.URL,
TraceID: r.TraceID,
AccessToken: r.AccessToken,
}
}
func fromMultipartReq(req *http.Request) (reqMeta, io.ReadCloser, error) {
meta := req.FormValue("meta")
if meta == "" {
return reqMeta{}, nil, errors.New("no meta field provided in multipart form submission")
}
var reqFn reqMeta
err := json.Unmarshal([]byte(meta), &reqFn)
if err != nil {
return reqMeta{}, nil, fmt.Errorf("failed to json unmarshal meta from multipart field: %w", err)
}
body, _, err := req.FormFile("body")
if err != nil {
return reqMeta{}, nil, fmt.Errorf("failed to read multipart body form file: %w", err)
}
return reqFn, body, nil
}
func fromJSONReq(req *http.Request) (reqMeta, io.ReadCloser, error) {
var r struct {
reqMeta
Body json.RawMessage `json:"body"`
}
payload, err := io.ReadAll(io.LimitReader(req.Body, 5*mb))
if err != nil {
return reqMeta{}, nil, fmt.Errorf("failed to read request body: %s", err)
}
if err = json.Unmarshal(payload, &r); err != nil {
return reqMeta{}, nil, fmt.Errorf("failed to unmarshal request body: %s", err)
}
return r.reqMeta, io.NopCloser(bytes.NewReader(r.Body)), nil
}
func writeResponse(logger *slog.Logger, w http.ResponseWriter, resp Response) error {
b, err := json.Marshal(struct {
Body json.Marshaler `json:"body,omitempty"`
Code int `json:"code,omitempty"`
Errors []APIError `json:"errors"`
Headers http.Header `json:"headers,omitempty"`
}{
Body: resp.Body,
Code: resp.StatusCode(),
Errors: resp.Errors,
Headers: resp.Header,
})
if err != nil {
logger.Error("failed to marshal json payload with body", "err", err)
}
if len(b) == 0 {
return nil
}
if code := resp.StatusCode(); code != 0 {
w.WriteHeader(code)
}
_, err = w.Write(b)
return err
}
func writeFile(logger *slog.Logger, r io.ReadCloser, filename string) (string, int, error) {
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return "", 0, fmt.Errorf("failed to open file: %w", err)
}
defer func() {
// just in case
_ = f.Close()
_ = r.Close()
}()
sizer, h := &sizeRecorder{w: f}, sha256.New()
mw := io.MultiWriter(sizer, h)
_, err = io.Copy(mw, r)
if err != nil {
return "", 0, fmt.Errorf("failed to write contents to file: %w", err)
}
err = r.Close()
if err != nil {
// we swallow the error here, there's nothing we can do about it...
logger.Error("failed to close file contents", "err", err)
}
err = f.Close()
if err != nil {
return "", 0, fmt.Errorf("failed to close file: %w", err)
}
sha256Hash := base64.StdEncoding.EncodeToString(h.Sum(nil))
return sha256Hash, sizer.n, nil
}
func port() int {
if v, _ := strconv.Atoi(os.Getenv(envPort)); v > 0 {
return v
}
return 8081
}
type sizeRecorder struct {
w io.Writer
n int
}
func (s *sizeRecorder) Write(p []byte) (int, error) {
n, err := s.w.Write(p)
s.n += n
return n, err
}