mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Make Dispatch cleaner
This commit is contained in:
@@ -25,53 +25,54 @@ func (p *JSONProtocol) IsStreamable() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *JSONProtocol) Dispatch(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 {
|
||||||
// this shouldn't happen
|
// this shouldn't happen
|
||||||
return respondWithError(
|
return err
|
||||||
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
|
||||||
}
|
}
|
||||||
if req.Body != nil {
|
if req.Body != nil {
|
||||||
_, err := io.WriteString(h.in, `"body":"`)
|
_, err := io.WriteString(h.in, `"body":"`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// this shouldn't happen
|
// this shouldn't happen
|
||||||
return respondWithError(
|
return err
|
||||||
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
|
||||||
}
|
}
|
||||||
_, err = io.Copy(h.in, req.Body)
|
_, err = io.Copy(h.in, req.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// this shouldn't happen
|
// this shouldn't happen
|
||||||
return respondWithError(
|
return err
|
||||||
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
|
||||||
}
|
}
|
||||||
_, err = io.WriteString(h.in, `",`)
|
_, err = io.WriteString(h.in, `",`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// this shouldn't happen
|
// this shouldn't happen
|
||||||
return respondWithError(
|
return err
|
||||||
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
|
||||||
}
|
}
|
||||||
defer req.Body.Close()
|
defer req.Body.Close()
|
||||||
}
|
}
|
||||||
_, err = io.WriteString(h.in, `"headers:"`)
|
_, err = io.WriteString(h.in, `"headers:"`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// this shouldn't happen
|
// this shouldn't happen
|
||||||
return respondWithError(
|
return err
|
||||||
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
|
||||||
}
|
}
|
||||||
err = json.NewEncoder(h.in).Encode(req.Header)
|
err = json.NewEncoder(h.in).Encode(req.Header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// this shouldn't happen
|
// this shouldn't happen
|
||||||
return respondWithError(
|
return err
|
||||||
w, fmt.Errorf("error marshalling JSONInput: %s", err.Error()))
|
|
||||||
}
|
}
|
||||||
_, err = io.WriteString(h.in, `"}`)
|
_, err = io.WriteString(h.in, `"}`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// this shouldn't happen
|
// this shouldn't happen
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
||||||
|
err := h.DumpJSON(w, req)
|
||||||
|
if err != nil {
|
||||||
return respondWithError(
|
return respondWithError(
|
||||||
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
jout := new(JSONIO)
|
jout := new(JSONIO)
|
||||||
dec := json.NewDecoder(h.out)
|
dec := json.NewDecoder(h.out)
|
||||||
if err := dec.Decode(jout); err != nil {
|
if err := dec.Decode(jout); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user