|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | 4 | "bytes" |
6 | 5 | "encoding/json" |
7 | 6 | "fmt" |
8 | | - "io" |
9 | 7 | "log" |
| 8 | + "net/http" |
10 | 9 | "os" |
11 | 10 | ) |
12 | 11 |
|
13 | 12 | type Person struct { |
14 | 13 | Name string `json:"name"` |
15 | 14 | } |
16 | 15 |
|
17 | | -type JSONInput struct { |
18 | | - Body string `json:"body"` |
19 | | -} |
20 | | - |
21 | | -type JSONOutput struct { |
22 | | - StatusCode int `json:"status"` |
23 | | - Body string `json:"body"` |
| 16 | +type JSON struct { |
| 17 | + Headers http.Header `json:"headers"` |
| 18 | + Body string `json:"body,omitempty"` |
| 19 | + StatusCode int `json:"status,omitempty"` |
24 | 20 | } |
25 | 21 |
|
26 | 22 | func main() { |
27 | 23 |
|
28 | | - enc := json.NewEncoder(os.Stdout) |
29 | | - r := bufio.NewReader(os.Stdin) |
| 24 | + stdin := json.NewDecoder(os.Stdin) |
| 25 | + stdout := json.NewEncoder(os.Stdout) |
| 26 | + stderr := json.NewEncoder(os.Stderr) |
30 | 27 | for { |
31 | | - var buf bytes.Buffer |
32 | | - in := &JSONInput{} |
33 | | - _, err := io.Copy(&buf, r) |
34 | | - if err != nil { |
35 | | - log.Fatalln(err) |
36 | | - } |
| 28 | + in := &JSON{} |
37 | 29 |
|
38 | | - err = json.Unmarshal(buf.Bytes(), in) |
| 30 | + err := stdin.Decode(in) |
39 | 31 | if err != nil { |
40 | | - log.Fatalln(err) |
| 32 | + log.Fatalf("Unable to decode incoming data: %s", err.Error()) |
| 33 | + fmt.Fprintf(os.Stderr, err.Error()) |
41 | 34 | } |
42 | | - |
43 | 35 | person := Person{} |
44 | | - if in.Body != "" { |
45 | | - if err := json.Unmarshal([]byte(in.Body), &person); err != nil { |
46 | | - log.Fatalln(err) |
| 36 | + stderr.Encode(in.Body) |
| 37 | + stderr.Encode(fmt.Sprintf(in.Body)) |
| 38 | + if len(in.Body) != 0 { |
| 39 | + if err := json.NewDecoder(bytes.NewReader([]byte(in.Body))).Decode(&person); err != nil { |
| 40 | + log.Fatalf("Unable to decode Person object data: %s", err.Error()) |
| 41 | + fmt.Fprintf(os.Stderr, err.Error()) |
47 | 42 | } |
48 | 43 | } |
49 | 44 | if person.Name == "" { |
50 | 45 | person.Name = "World" |
51 | 46 | } |
52 | 47 |
|
53 | 48 | mapResult := map[string]string{"message": fmt.Sprintf("Hello %s", person.Name)} |
54 | | - out := &JSONOutput{StatusCode: 200} |
55 | | - b, _ := json.Marshal(mapResult) |
56 | | - out.Body = string(b) |
57 | | - if err := enc.Encode(out); err != nil { |
58 | | - log.Fatalln(err) |
| 49 | + b, err := json.Marshal(mapResult) |
| 50 | + if err != nil { |
| 51 | + log.Fatalf("Unable to marshal JSON response body: %s", err.Error()) |
| 52 | + fmt.Fprintf(os.Stderr, err.Error()) |
| 53 | + } |
| 54 | + out := &JSON{ |
| 55 | + StatusCode: http.StatusOK, |
| 56 | + Body: string(b), |
| 57 | + } |
| 58 | + stderr.Encode(out) |
| 59 | + if err := stdout.Encode(out); err != nil { |
| 60 | + log.Fatalf("Unable to encode JSON response: %s", err.Error()) |
| 61 | + fmt.Fprintf(os.Stderr, err.Error()) |
59 | 62 | } |
60 | 63 | } |
61 | 64 | } |
0 commit comments