mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Addressing comments
What's new? - unmarshal JSON response only in case of HTTP response writer
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -51,45 +52,35 @@ func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
|||||||
return respondWithError(
|
return respondWithError(
|
||||||
w, fmt.Errorf("error marshalling JSONInput: %s", err.Error()))
|
w, fmt.Errorf("error marshalling JSONInput: %s", err.Error()))
|
||||||
}
|
}
|
||||||
|
// TODO: write in chunks, how big should chunk be?
|
||||||
_, err = h.in.Write(b)
|
_, err = h.in.Write(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return respondWithError(
|
return respondWithError(
|
||||||
w, fmt.Errorf("error writing JSON object to function's STDIN: %s", err.Error()))
|
w, fmt.Errorf("error writing JSON object to function's STDIN: %s", err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// this has to be done for pulling out:
|
|
||||||
// - status code
|
|
||||||
// - body
|
|
||||||
jout := new(JSONIO)
|
|
||||||
dec := json.NewDecoder(h.out)
|
|
||||||
if err := dec.Decode(jout); err != nil {
|
|
||||||
return respondWithError(
|
|
||||||
w, fmt.Errorf("unable to decode JSON response object: %s", err.Error()))
|
|
||||||
}
|
|
||||||
|
|
||||||
if rw, ok := w.(http.ResponseWriter); ok {
|
if rw, ok := w.(http.ResponseWriter); ok {
|
||||||
rw.WriteHeader(jout.StatusCode)
|
// this has to be done for pulling out:
|
||||||
outBytes, err := json.Marshal(jout.Body)
|
// - status code
|
||||||
if err != nil {
|
// - body
|
||||||
|
jout := new(JSONIO)
|
||||||
|
dec := json.NewDecoder(h.out)
|
||||||
|
if err := dec.Decode(jout); err != nil {
|
||||||
return respondWithError(
|
return respondWithError(
|
||||||
w, fmt.Errorf("unable to marshal JSON response object: %s", err.Error()))
|
w, fmt.Errorf("unable to decode JSON response object: %s", err.Error()))
|
||||||
}
|
}
|
||||||
_, err = rw.Write(outBytes) // TODO timeout
|
rw.WriteHeader(jout.StatusCode)
|
||||||
|
_, err = rw.Write([]byte(jout.Body)) // TODO timeout
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return respondWithError(
|
return respondWithError(
|
||||||
w, fmt.Errorf("unable to write JSON response object: %s", err.Error()))
|
w, fmt.Errorf("unable to write JSON response object: %s", err.Error()))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// logs can just copy the full thing in there, headers and all.
|
// logs can just copy the full thing in there, headers and all.
|
||||||
outBytes, err := json.Marshal(jout.Body)
|
_, err = io.Copy(w, bufio.NewReader(h.out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return respondWithError(
|
return respondWithError(
|
||||||
w, fmt.Errorf("unable to marshal JSON response object: %s", err.Error()))
|
w, fmt.Errorf("error reading function response: %s", err.Error()))
|
||||||
}
|
|
||||||
_, err = w.Write(outBytes) // TODO timeout
|
|
||||||
if err != nil {
|
|
||||||
return respondWithError(
|
|
||||||
w, fmt.Errorf("unable to write JSON response object: %s", err.Error()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user