-
Notifications
You must be signed in to change notification settings - Fork 405
/
status.go
54 lines (45 loc) · 959 Bytes
/
status.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
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"log"
fdk "github.com/fnproject/fdk-go"
)
func main() {
fdk.Handle(fdk.HandlerFunc(myHandler))
}
func myHandler(ctx context.Context, in io.Reader, out io.Writer) {
var input map[string]interface{}
body, err := ioutil.ReadAll(in)
if err != nil {
log.Print("could not read input")
fdk.WriteStatus(out, 530)
return
}
err = json.Unmarshal(body, &input)
if err != nil {
log.Print("could not unmarshal json input")
fdk.WriteStatus(out, 531)
return
}
output, err := json.Marshal(&input)
if err != nil {
log.Print("could not marshal json output")
fdk.WriteStatus(out, 532)
return
}
written, err := io.Copy(out, bytes.NewReader(output))
if err != nil {
log.Print("could not write output")
fdk.WriteStatus(out, 533)
return
}
if written != int64(len(output)) {
log.Print("partial write of output")
fdk.WriteStatus(out, 534)
return
}
}