-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (87 loc) · 2.62 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
106
107
108
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
flag "github.com/spf13/pflag"
)
type config struct {
port int
}
var c config
func init() {
flag.IntVarP(&c.port, "port", "p", 8080, "the listen port")
flag.Parse()
}
func main() {
// exempt from required headers, etc.
http.HandleFunc("/healthcheck", func(w http.ResponseWriter, t *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/", handler)
listenAddr := fmt.Sprintf(":%d", c.port)
fmt.Printf("Listening on %s...\n", listenAddr)
if err := http.ListenAndServe(listenAddr, nil); err != nil {
fmt.Println("server error: ", err)
os.Exit(1)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Max-Age", "86400")
w.WriteHeader(http.StatusNoContent)
default:
responseBody, responseCode := buildResponse(r.Header)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(responseCode)
w.Write([]byte(responseBody))
}
}
func buildResponse(header map[string][]string) (string, int) {
responseBody, err := validateResponseBody(header)
if err != nil {
return err.Error(), http.StatusBadRequest
}
responseCode, err := validateResponseCode(header)
if err != nil {
return err.Error(), http.StatusBadRequest
}
return responseBody, responseCode
}
func validateResponseBody(header map[string][]string) (string, error) {
if _, exists := header["X-Response-Json"]; !exists {
return "", errorResponseFormatter("x-response-json must be set on the request")
}
if !isJSON(header["X-Response-Json"][0]) {
return "", errorResponseFormatter("x-response-json must be valid JSON")
}
return header["X-Response-Json"][0], nil
}
func validateResponseCode(header map[string][]string) (int, error) {
if val, exists := header["X-Response-Code"]; exists {
code, err := strconv.Atoi(val[0])
if err != nil {
return http.StatusBadRequest, errorResponseFormatter("x-response-code must be a number")
}
if !(code >= 100 && code <= 599) {
return http.StatusBadRequest, errorResponseFormatter("x-response-code must be between 100 and 599")
}
return code, nil
}
return http.StatusOK, nil
}
func isJSON(s string) bool {
var j json.RawMessage
return json.Unmarshal([]byte(s), &j) == nil
}
func errorResponseFormatter(msg string) error {
return fmt.Errorf(`{"error":"%s"}`, msg)
}