Cleaning up code

Getting rid of request url, call id, method: all of them are
 redundant and available through env
This commit is contained in:
Denis Makogon
2017-09-26 15:21:58 +03:00
parent b6b9b55ca9
commit ecaa5eefbf
4 changed files with 14 additions and 51 deletions

View File

@@ -11,9 +11,6 @@ import (
// JSONInput is what's sent into the function // JSONInput is what's sent into the function
// All HTTP request headers should be set in env // All HTTP request headers should be set in env
type JSONInput struct { type JSONInput struct {
RequestURL string `json:"request_url"`
CallID string `json:"call_id"`
Method string `json:"method"`
Body string `json:"body"` Body string `json:"body"`
} }
@@ -35,10 +32,6 @@ func (p *JSONProtocol) IsStreamable() bool {
} }
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error { func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
reqURL := req.Header.Get("REQUEST_URL")
method := req.Header.Get("METHOD")
callID := req.Header.Get("CALL_ID")
// TODO content-length or chunked encoding // TODO content-length or chunked encoding
var body bytes.Buffer var body bytes.Buffer
if req.Body != nil { if req.Body != nil {
@@ -50,9 +43,6 @@ func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
// convert to JSON func format // convert to JSON func format
jin := &JSONInput{ jin := &JSONInput{
RequestURL: reqURL,
Method: method,
CallID: callID,
Body: body.String(), Body: body.String(),
} }
b, err := json.Marshal(jin) b, err := json.Marshal(jin)

View File

@@ -62,7 +62,7 @@ The header keys and values would be populated with information about the functio
Pros: Pros:
* Streamable * Supports streaming
* Common format * Common format
Cons: Cons:
@@ -78,21 +78,10 @@ An easy to parse JSON structure.
```json ```json
{ {
"request_url": "http://....",
"call_id": "abc123",
"method": "GET",
"body": { "body": {
"some": "input" "some": "input"
} }
} }
{
"request_url":"http://....",
"call_id": "edf456",
"method": "GET",
"body": {
"other": "input"
}
}
``` ```
Pros: Pros:

View File

@@ -12,42 +12,25 @@ type Person struct {
} }
type JSONInput struct { type JSONInput struct {
RequestURL string `json:"request_url"`
CallID string `json:"call_id"`
Method string `json:"method"`
Body string `json:"body"` Body string `json:"body"`
} }
func (a *JSONInput) String() string {
return fmt.Sprintf("request_url=%s\ncall_id=%s\nmethod=%s\n\nbody=%s",
a.RequestURL, a.CallID, a.Method, a.Body)
}
type JSONOutput struct { type JSONOutput struct {
StatusCode int `json:"status"` StatusCode int `json:"status"`
Body string `json:"body"` Body string `json:"body"`
} }
func main() { func main() {
// p := &Person{Name: "World"}
// json.Unmarshal(os.Stdin).Decode(p)
// mapD := map[string]string{"message": fmt.Sprintf("Hello %s", p.Name)}
// mapB, _ := json.Marshal(mapD)
// fmt.Println(string(mapB))
dec := json.NewDecoder(os.Stdin) dec := json.NewDecoder(os.Stdin)
enc := json.NewEncoder(os.Stdout) enc := json.NewEncoder(os.Stdout)
var loopCounter = 0
for { for {
loopCounter++
log.Println("loopCounter:", loopCounter)
in := &JSONInput{} in := &JSONInput{}
if err := dec.Decode(in); err != nil { if err := dec.Decode(in); err != nil {
log.Fatalln(err) log.Fatalln(err)
return return
} }
log.Println("JSONInput: ", in)
person := Person{} person := Person{}
if in.Body != "" { if in.Body != "" {
@@ -55,8 +38,9 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
} }
if person.Name == "" {
log.Println("Person: ", person) person.Name = "World"
}
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} out := &JSONOutput{StatusCode: 200}