mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Addressing certain comments
What's new? - better error handling - still need to decode JSON from function because we need status code and body - prevent request body to be a problem by deferring its close - moving examples around: putting http and json samples into one folder
This commit is contained in:
@@ -6,20 +6,14 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// JSONInput is what's sent into the function
|
||||
// JSONIn is what's sent into the function
|
||||
// All HTTP request headers should be set in env
|
||||
type JSONInput struct {
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
// JSONOutput function must return this format
|
||||
// StatusCode value must be a HTTP status code
|
||||
type JSONOutput struct {
|
||||
StatusCode int `json:"status"`
|
||||
Body string `json:"body"`
|
||||
type JSONIO struct {
|
||||
Headers http.Header `json:"headers,omitempty"`
|
||||
Body string `json:"body"`
|
||||
StatusCode int `json:"status_code,omitempty"`
|
||||
}
|
||||
|
||||
// JSONProtocol converts stdin/stdout streams from HTTP into JSON format.
|
||||
@@ -32,88 +26,88 @@ func (p *JSONProtocol) IsStreamable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type ErrMsg struct {
|
||||
Err Error `json:"error"`
|
||||
}
|
||||
|
||||
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
||||
var body bytes.Buffer
|
||||
if req.Body != nil {
|
||||
var dest io.Writer = &body
|
||||
|
||||
// TODO copy w/ ctx
|
||||
nBytes, _ := strconv.ParseInt(
|
||||
req.Header.Get("Content-Length"), 10, 64)
|
||||
_, err := io.Copy(dest, io.LimitReader(req.Body, nBytes))
|
||||
_, err := io.Copy(dest, req.Body)
|
||||
if err != nil {
|
||||
respondWithError(w, err)
|
||||
return err
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
||||
}
|
||||
defer req.Body.Close()
|
||||
}
|
||||
|
||||
// convert to JSON func format
|
||||
jin := &JSONInput{
|
||||
Body: body.String(),
|
||||
jin := &JSONIO{
|
||||
Headers: req.Header,
|
||||
Body: body.String(),
|
||||
}
|
||||
b, err := json.Marshal(jin)
|
||||
if err != nil {
|
||||
// this shouldn't happen
|
||||
err = fmt.Errorf("error marshalling JSONInput: %v", err)
|
||||
respondWithError(w, err)
|
||||
return err
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("error marshalling JSONInput: %s", err.Error()))
|
||||
}
|
||||
_, err = h.in.Write(b)
|
||||
if err != nil {
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("error writing JSON object to function's STDIN: %s", err.Error()))
|
||||
}
|
||||
h.in.Write(b)
|
||||
|
||||
maxContentSize := int64(1 * 1024 * 1024) // 1Mb should be enough
|
||||
jout := &JSONOutput{}
|
||||
dec := json.NewDecoder(io.LimitReader(h.out, maxContentSize))
|
||||
// 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 {
|
||||
err = fmt.Errorf("Unable to decode JSON response object: %s", err.Error())
|
||||
respondWithError(w, err)
|
||||
return err
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("unable to decode JSON response object: %s", err.Error()))
|
||||
}
|
||||
|
||||
if rw, ok := w.(http.ResponseWriter); ok {
|
||||
b, err = json.Marshal(jout.Body)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error unmarshalling JSON body: %s", err.Error())
|
||||
respondWithError(w, err)
|
||||
return err
|
||||
}
|
||||
rw.WriteHeader(jout.StatusCode)
|
||||
rw.Write(b) // TODO timeout
|
||||
outBytes, err := json.Marshal(jout.Body)
|
||||
if err != nil {
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("unable to marshal JSON response object: %s", err.Error()))
|
||||
}
|
||||
_, err = rw.Write(outBytes) // TODO timeout
|
||||
if err != nil {
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("unable to write JSON response object: %s", err.Error()))
|
||||
}
|
||||
} else {
|
||||
// logs can just copy the full thing in there, headers and all.
|
||||
b, err = json.Marshal(jout)
|
||||
outBytes, err := json.Marshal(jout.Body)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error unmarshalling JSON response: %s", err.Error())
|
||||
respondWithError(w, err)
|
||||
return err
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("unable to marshal JSON response object: %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()))
|
||||
}
|
||||
w.Write(b) // TODO timeout
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func respondWithError(w io.Writer, err error) {
|
||||
errMsg := ErrMsg{
|
||||
Err: Error{
|
||||
Message: err.Error(),
|
||||
},
|
||||
}
|
||||
b, _ := json.Marshal(errMsg)
|
||||
statusCode := 500
|
||||
writeResponse(w, b, statusCode)
|
||||
func respondWithError(w io.Writer, err error) error {
|
||||
writeResponse(w, []byte(err.Error()), http.StatusInternalServerError)
|
||||
return err
|
||||
}
|
||||
|
||||
func writeResponse(w io.Writer, b []byte, statusCode int) {
|
||||
if rw, ok := w.(http.ResponseWriter); ok {
|
||||
rw.WriteHeader(statusCode)
|
||||
rw.Write(b) // TODO timeout
|
||||
_, err := rw.Write(b) // TODO timeout
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unable to write JSON response object: %s", err.Error())
|
||||
respondWithError(w, err)
|
||||
}
|
||||
} else {
|
||||
// logs can just copy the full thing in there, headers and all.
|
||||
w.Write(b) // TODO timeout
|
||||
|
||||
Reference in New Issue
Block a user