mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Revisiting request body processing
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -25,6 +26,23 @@ func (p *JSONProtocol) IsStreamable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type RequestEncoder struct {
|
||||
*json.Encoder
|
||||
}
|
||||
|
||||
func (re *RequestEncoder) EncodeRequest(rq *http.Request) error {
|
||||
bb := new(bytes.Buffer)
|
||||
_, err := bb.ReadFrom(rq.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer bb.Reset()
|
||||
return re.Encode(JSONIO{
|
||||
Headers: rq.Header,
|
||||
Body: bb.String(),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
|
||||
_, err := io.WriteString(h.in, `{`)
|
||||
if err != nil {
|
||||
@@ -68,7 +86,9 @@ func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
|
||||
}
|
||||
|
||||
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
||||
err := h.DumpJSON(w, req)
|
||||
ce := RequestEncoder{json.NewEncoder(h.in)}
|
||||
err := ce.EncodeRequest(req)
|
||||
//err := h.DumpJSON(w, req)
|
||||
if err != nil {
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("unable to write JSON into STDIN: %s", err.Error()))
|
||||
|
||||
8
examples/formats/http/go/Dockerfile
Normal file
8
examples/formats/http/go/Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM fnproject/go:dev as build-stage
|
||||
WORKDIR /function
|
||||
ADD . /src
|
||||
RUN cd /src && go build -o func
|
||||
FROM fnproject/go
|
||||
WORKDIR /function
|
||||
COPY --from=build-stage /src/func /function/
|
||||
ENTRYPOINT ["./func"]
|
||||
@@ -1,9 +1,8 @@
|
||||
FROM jjanzic/docker-python3-opencv
|
||||
FROM python:3.6.2
|
||||
|
||||
RUN mkdir /code
|
||||
ADD . /code/
|
||||
WORKDIR /code
|
||||
RUN pip3 install -r requirements.txt
|
||||
|
||||
WORKDIR /code/
|
||||
ENTRYPOINT ["python3", "func.py"]
|
||||
|
||||
6
examples/formats/json/go/.gitignore
vendored
6
examples/formats/json/go/.gitignore
vendored
@@ -1,6 +0,0 @@
|
||||
vendor/
|
||||
/go
|
||||
/app
|
||||
/__uberscript__
|
||||
|
||||
func.yaml
|
||||
8
examples/formats/json/go/Dockerfile
Normal file
8
examples/formats/json/go/Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM fnproject/go:dev as build-stage
|
||||
WORKDIR /function
|
||||
ADD . /src
|
||||
RUN cd /src && go build -o func
|
||||
FROM fnproject/go
|
||||
WORKDIR /function
|
||||
COPY --from=build-stage /src/func /function/
|
||||
ENTRYPOINT ["./func"]
|
||||
@@ -1,12 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -14,36 +13,32 @@ type Person struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type JSONInput struct {
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
type JSONOutput struct {
|
||||
StatusCode int `json:"status"`
|
||||
Body string `json:"body"`
|
||||
type JSON struct {
|
||||
Headers http.Header `json:"headers"`
|
||||
Body string `json:"body,omitempty"`
|
||||
StatusCode int `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
stdin := json.NewDecoder(os.Stdin)
|
||||
stdout := json.NewEncoder(os.Stdout)
|
||||
stderr := json.NewEncoder(os.Stderr)
|
||||
for {
|
||||
var buf bytes.Buffer
|
||||
in := &JSONInput{}
|
||||
_, err := io.Copy(&buf, r)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
in := &JSON{}
|
||||
|
||||
err = json.Unmarshal(buf.Bytes(), in)
|
||||
err := stdin.Decode(in)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
log.Fatalf("Unable to decode incoming data: %s", err.Error())
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
}
|
||||
|
||||
person := Person{}
|
||||
if in.Body != "" {
|
||||
if err := json.Unmarshal([]byte(in.Body), &person); err != nil {
|
||||
log.Fatalln(err)
|
||||
stderr.Encode(in.Body)
|
||||
stderr.Encode(fmt.Sprintf(in.Body))
|
||||
if len(in.Body) != 0 {
|
||||
if err := json.NewDecoder(bytes.NewReader([]byte(in.Body))).Decode(&person); err != nil {
|
||||
log.Fatalf("Unable to decode Person object data: %s", err.Error())
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
}
|
||||
}
|
||||
if person.Name == "" {
|
||||
@@ -51,11 +46,19 @@ func main() {
|
||||
}
|
||||
|
||||
mapResult := map[string]string{"message": fmt.Sprintf("Hello %s", person.Name)}
|
||||
out := &JSONOutput{StatusCode: 200}
|
||||
b, _ := json.Marshal(mapResult)
|
||||
out.Body = string(b)
|
||||
if err := enc.Encode(out); err != nil {
|
||||
log.Fatalln(err)
|
||||
b, err := json.Marshal(mapResult)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to marshal JSON response body: %s", err.Error())
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
}
|
||||
out := &JSON{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: string(b),
|
||||
}
|
||||
stderr.Encode(out)
|
||||
if err := stdout.Encode(out); err != nil {
|
||||
log.Fatalf("Unable to encode JSON response: %s", err.Error())
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
7
examples/formats/json/go/func.yaml
Normal file
7
examples/formats/json/go/func.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
name: fnproject/hot-json-go
|
||||
version: 0.0.1
|
||||
runtime: docker
|
||||
type: sync
|
||||
memory: 256
|
||||
format: json
|
||||
path: /hot-json-go
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"Name": "Johnny"
|
||||
"name": "Johnny"
|
||||
}
|
||||
|
||||
8
examples/formats/json/python/Dockerfile
Normal file
8
examples/formats/json/python/Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM python:3.6.2
|
||||
|
||||
RUN mkdir /code
|
||||
ADD . /code/
|
||||
WORKDIR /code
|
||||
RUN pip3 install -r requirements.txt
|
||||
|
||||
ENTRYPOINT ["python3", "func.py"]
|
||||
@@ -3,5 +3,5 @@ version: 0.0.1
|
||||
runtime: docker
|
||||
type: sync
|
||||
memory: 256
|
||||
format: http
|
||||
format: json
|
||||
path: /hot-json-python
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
name: hotfunction-http
|
||||
version: 0.0.10
|
||||
runtime: go
|
||||
entrypoint: ./func
|
||||
format: http
|
||||
path: /hotfn-go
|
||||
Reference in New Issue
Block a user