Files
fn-serverless/examples/formats/json/go/func.go
Denis Makogon 783490dc79 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
2017-10-07 00:43:07 +03:00

62 lines
1019 B
Go

package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
)
type Person struct {
Name string `json:"name"`
}
type JSONInput struct {
Body string `json:"body"`
}
type JSONOutput struct {
StatusCode int `json:"status"`
Body string `json:"body"`
}
func main() {
enc := json.NewEncoder(os.Stdout)
r := bufio.NewReader(os.Stdin)
for {
var buf bytes.Buffer
in := &JSONInput{}
_, err := io.Copy(&buf, r)
if err != nil {
log.Fatalln(err)
}
err = json.Unmarshal(buf.Bytes(), in)
if err != nil {
log.Fatalln(err)
}
person := Person{}
if in.Body != "" {
if err := json.Unmarshal([]byte(in.Body), &person); err != nil {
log.Fatalln(err)
}
}
if person.Name == "" {
person.Name = "World"
}
mapResult := map[string]string{"message": fmt.Sprintf("Hello %s", person.Name)}
out := &JSONOutput{StatusCode: 200}
b, _ := json.Marshal(mapResult)
out.Body = string(b)
if err := enc.Encode(out); err != nil {
log.Fatalln(err)
}
}
}