Skip to content

Commit c2ee67f

Browse files
committed
Revisiting request body processing
1 parent 1f589d6 commit c2ee67f

12 files changed

Lines changed: 86 additions & 45 deletions

File tree

api/agent/protocol/json.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package protocol
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -25,6 +26,23 @@ func (p *JSONProtocol) IsStreamable() bool {
2526
return true
2627
}
2728

29+
type RequestEncoder struct {
30+
*json.Encoder
31+
}
32+
33+
func (re *RequestEncoder) EncodeRequest(rq *http.Request) error {
34+
bb := new(bytes.Buffer)
35+
_, err := bb.ReadFrom(rq.Body)
36+
if err != nil {
37+
return err
38+
}
39+
defer bb.Reset()
40+
return re.Encode(JSONIO{
41+
Headers: rq.Header,
42+
Body: bb.String(),
43+
})
44+
}
45+
2846
func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
2947
_, err := io.WriteString(h.in, `{`)
3048
if err != nil {
@@ -68,7 +86,9 @@ func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
6886
}
6987

7088
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
71-
err := h.DumpJSON(w, req)
89+
ce := RequestEncoder{json.NewEncoder(h.in)}
90+
err := ce.EncodeRequest(req)
91+
//err := h.DumpJSON(w, req)
7292
if err != nil {
7393
return respondWithError(
7494
w, fmt.Errorf("unable to write JSON into STDIN: %s", err.Error()))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM fnproject/go:dev as build-stage
2+
WORKDIR /function
3+
ADD . /src
4+
RUN cd /src && go build -o func
5+
FROM fnproject/go
6+
WORKDIR /function
7+
COPY --from=build-stage /src/func /function/
8+
ENTRYPOINT ["./func"]
File renamed without changes.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
FROM jjanzic/docker-python3-opencv
1+
FROM python:3.6.2
22

33
RUN mkdir /code
44
ADD . /code/
55
WORKDIR /code
66
RUN pip3 install -r requirements.txt
77

8-
WORKDIR /code/
98
ENTRYPOINT ["python3", "func.py"]

examples/formats/json/go/.gitignore

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM fnproject/go:dev as build-stage
2+
WORKDIR /function
3+
ADD . /src
4+
RUN cd /src && go build -o func
5+
FROM fnproject/go
6+
WORKDIR /function
7+
COPY --from=build-stage /src/func /function/
8+
ENTRYPOINT ["./func"]

examples/formats/json/go/func.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,64 @@
11
package main
22

33
import (
4-
"bufio"
54
"bytes"
65
"encoding/json"
76
"fmt"
8-
"io"
97
"log"
8+
"net/http"
109
"os"
1110
)
1211

1312
type Person struct {
1413
Name string `json:"name"`
1514
}
1615

17-
type JSONInput struct {
18-
Body string `json:"body"`
19-
}
20-
21-
type JSONOutput struct {
22-
StatusCode int `json:"status"`
23-
Body string `json:"body"`
16+
type JSON struct {
17+
Headers http.Header `json:"headers"`
18+
Body string `json:"body,omitempty"`
19+
StatusCode int `json:"status,omitempty"`
2420
}
2521

2622
func main() {
2723

28-
enc := json.NewEncoder(os.Stdout)
29-
r := bufio.NewReader(os.Stdin)
24+
stdin := json.NewDecoder(os.Stdin)
25+
stdout := json.NewEncoder(os.Stdout)
26+
stderr := json.NewEncoder(os.Stderr)
3027
for {
31-
var buf bytes.Buffer
32-
in := &JSONInput{}
33-
_, err := io.Copy(&buf, r)
34-
if err != nil {
35-
log.Fatalln(err)
36-
}
28+
in := &JSON{}
3729

38-
err = json.Unmarshal(buf.Bytes(), in)
30+
err := stdin.Decode(in)
3931
if err != nil {
40-
log.Fatalln(err)
32+
log.Fatalf("Unable to decode incoming data: %s", err.Error())
33+
fmt.Fprintf(os.Stderr, err.Error())
4134
}
42-
4335
person := Person{}
44-
if in.Body != "" {
45-
if err := json.Unmarshal([]byte(in.Body), &person); err != nil {
46-
log.Fatalln(err)
36+
stderr.Encode(in.Body)
37+
stderr.Encode(fmt.Sprintf(in.Body))
38+
if len(in.Body) != 0 {
39+
if err := json.NewDecoder(bytes.NewReader([]byte(in.Body))).Decode(&person); err != nil {
40+
log.Fatalf("Unable to decode Person object data: %s", err.Error())
41+
fmt.Fprintf(os.Stderr, err.Error())
4742
}
4843
}
4944
if person.Name == "" {
5045
person.Name = "World"
5146
}
5247

5348
mapResult := map[string]string{"message": fmt.Sprintf("Hello %s", person.Name)}
54-
out := &JSONOutput{StatusCode: 200}
55-
b, _ := json.Marshal(mapResult)
56-
out.Body = string(b)
57-
if err := enc.Encode(out); err != nil {
58-
log.Fatalln(err)
49+
b, err := json.Marshal(mapResult)
50+
if err != nil {
51+
log.Fatalf("Unable to marshal JSON response body: %s", err.Error())
52+
fmt.Fprintf(os.Stderr, err.Error())
53+
}
54+
out := &JSON{
55+
StatusCode: http.StatusOK,
56+
Body: string(b),
57+
}
58+
stderr.Encode(out)
59+
if err := stdout.Encode(out); err != nil {
60+
log.Fatalf("Unable to encode JSON response: %s", err.Error())
61+
fmt.Fprintf(os.Stderr, err.Error())
5962
}
6063
}
6164
}

examples/formats/json/go/func.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: fnproject/hot-json-go
2+
version: 0.0.1
3+
runtime: docker
4+
type: sync
5+
memory: 256
6+
format: json
7+
path: /hot-json-go
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"Name": "Johnny"
2+
"name": "Johnny"
33
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.6.2
2+
3+
RUN mkdir /code
4+
ADD . /code/
5+
WORKDIR /code
6+
RUN pip3 install -r requirements.txt
7+
8+
ENTRYPOINT ["python3", "func.py"]

0 commit comments

Comments
 (0)