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
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -25,6 +26,23 @@ func (p *JSONProtocol) IsStreamable() bool {
|
|||||||
return true
|
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 {
|
func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
|
||||||
_, err := io.WriteString(h.in, `{`)
|
_, err := io.WriteString(h.in, `{`)
|
||||||
if err != nil {
|
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 {
|
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 {
|
if err != nil {
|
||||||
return respondWithError(
|
return respondWithError(
|
||||||
w, fmt.Errorf("unable to write JSON into STDIN: %s", err.Error()))
|
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
|
RUN mkdir /code
|
||||||
ADD . /code/
|
ADD . /code/
|
||||||
WORKDIR /code
|
WORKDIR /code
|
||||||
RUN pip3 install -r requirements.txt
|
RUN pip3 install -r requirements.txt
|
||||||
|
|
||||||
WORKDIR /code/
|
|
||||||
ENTRYPOINT ["python3", "func.py"]
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,36 +13,32 @@ type Person struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type JSONInput struct {
|
type JSON struct {
|
||||||
Body string `json:"body"`
|
Headers http.Header `json:"headers"`
|
||||||
}
|
Body string `json:"body,omitempty"`
|
||||||
|
StatusCode int `json:"status,omitempty"`
|
||||||
type JSONOutput struct {
|
|
||||||
StatusCode int `json:"status"`
|
|
||||||
Body string `json:"body"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
enc := json.NewEncoder(os.Stdout)
|
stdin := json.NewDecoder(os.Stdin)
|
||||||
r := bufio.NewReader(os.Stdin)
|
stdout := json.NewEncoder(os.Stdout)
|
||||||
|
stderr := json.NewEncoder(os.Stderr)
|
||||||
for {
|
for {
|
||||||
var buf bytes.Buffer
|
in := &JSON{}
|
||||||
in := &JSONInput{}
|
|
||||||
_, err := io.Copy(&buf, r)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(buf.Bytes(), in)
|
err := stdin.Decode(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalf("Unable to decode incoming data: %s", err.Error())
|
||||||
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
person := Person{}
|
person := Person{}
|
||||||
if in.Body != "" {
|
stderr.Encode(in.Body)
|
||||||
if err := json.Unmarshal([]byte(in.Body), &person); err != nil {
|
stderr.Encode(fmt.Sprintf(in.Body))
|
||||||
log.Fatalln(err)
|
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 == "" {
|
if person.Name == "" {
|
||||||
@@ -51,11 +46,19 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mapResult := map[string]string{"message": fmt.Sprintf("Hello %s", person.Name)}
|
mapResult := map[string]string{"message": fmt.Sprintf("Hello %s", person.Name)}
|
||||||
out := &JSONOutput{StatusCode: 200}
|
b, err := json.Marshal(mapResult)
|
||||||
b, _ := json.Marshal(mapResult)
|
if err != nil {
|
||||||
out.Body = string(b)
|
log.Fatalf("Unable to marshal JSON response body: %s", err.Error())
|
||||||
if err := enc.Encode(out); err != nil {
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
log.Fatalln(err)
|
}
|
||||||
|
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
|
runtime: docker
|
||||||
type: sync
|
type: sync
|
||||||
memory: 256
|
memory: 256
|
||||||
format: http
|
format: json
|
||||||
path: /hot-json-python
|
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