forked from digitalocean/sample-golang
/
main.go
105 lines (94 loc) · 2.73 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
_ "github.com/joho/godotenv/autoload"
)
func logRequest(r *http.Request) {
uri := r.RequestURI
method := r.Method
fmt.Println("Got request!", method, uri)
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
logRequest(r)
fmt.Fprintf(w, "Hello! you've requested %s\n", r.URL.Path)
})
http.HandleFunc("/cached", func(w http.ResponseWriter, r *http.Request) {
logRequest(r)
maxAgeParams, ok := r.URL.Query()["max-age"]
if ok && len(maxAgeParams) > 0 {
maxAge, _ := strconv.Atoi(maxAgeParams[0])
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", maxAge))
}
responseHeaderParams, ok := r.URL.Query()["headers"]
if ok {
for _, header := range responseHeaderParams {
h := strings.Split(header, ":")
w.Header().Set(h[0], strings.TrimSpace(h[1]))
}
}
statusCodeParams, ok := r.URL.Query()["status"]
if ok {
statusCode, _ := strconv.Atoi(statusCodeParams[0])
w.WriteHeader(statusCode)
}
fmt.Fprintf(w, "Hello! you've requested %s\n", r.URL.Path)
})
http.HandleFunc("/headers", func(w http.ResponseWriter, r *http.Request) {
logRequest(r)
keys, ok := r.URL.Query()["key"]
if ok && len(keys) > 0 {
fmt.Fprint(w, r.Header.Get(keys[0]))
return
}
headers := []string{}
headers = append(headers, fmt.Sprintf("host=%s", r.Host))
for key, values := range r.Header {
headers = append(headers, fmt.Sprintf("%s=%s", key, strings.Join(values, ",")))
}
fmt.Fprint(w, strings.Join(headers, "\n"))
})
http.HandleFunc("/env", func(w http.ResponseWriter, r *http.Request) {
logRequest(r)
keys, ok := r.URL.Query()["key"]
if ok && len(keys) > 0 {
fmt.Fprint(w, os.Getenv(keys[0]))
return
}
envs := []string{}
envs = append(envs, os.Environ()...)
fmt.Fprint(w, strings.Join(envs, "\n"))
})
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
logRequest(r)
codeParams, ok := r.URL.Query()["code"]
if ok && len(codeParams) > 0 {
statusCode, _ := strconv.Atoi(codeParams[0])
if statusCode >= 200 && statusCode < 600 {
w.WriteHeader(statusCode)
}
}
fmt.Fprintf(w, "Hello! you've requested %s\n", r.URL.Path)
})
port := os.Getenv("PORT")
for _, encodedRoute := range strings.Split(os.Getenv("ROUTES"), ",") {
if encodedRoute == "" {
continue
}
pathAndBody := strings.SplitN(encodedRoute, "=", 2)
path, body := pathAndBody[0], pathAndBody[1]
http.HandleFunc("/"+path, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, body)
})
}
bindAddr := fmt.Sprintf(":%s", port)
fmt.Printf("==> Server listening at %s 🚀\n", bindAddr)
err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
panic(err)
}
}