forked from rnzsgh/eks-workshop-sample-api-service-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (43 loc) · 1020 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"sort"
"strings"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
f := fib()
res := &response{Message: "Hello World Darren Lin!"}
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
res.EnvVars = append(res.EnvVars, pair[0]+"="+pair[1])
}
sort.Strings(res.EnvVars)
for i := 1; i <= 90; i++ {
res.Fib = append(res.Fib, f())
}
// Beautify the JSON output
out, _ := json.MarshalIndent(res, "", " ")
// Normally this would be application/json, but we don't want to prompt downloads
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, string(out))
fmt.Println("Hello world - the log message")
})
http.ListenAndServe(":8080", nil)
}
type response struct {
Message string `json:"message"`
EnvVars []string `json:"env"`
Fib []int `json:"fib"`
}
func fib() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}