-
Notifications
You must be signed in to change notification settings - Fork 0
/
truth.go
133 lines (113 loc) · 2.94 KB
/
truth.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Minimal server to respond to requests with JSON
package main
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var routes map[string]func(http.ResponseWriter, *http.Request)
var port = "4242"
const httpReturnRequestedCodePath = "/httpCode/"
type router struct{}
func handleHTTPCode(w http.ResponseWriter, r *http.Request) {
n, err := strconv.Atoi(r.URL.Path[len(httpReturnRequestedCodePath):])
log(r, n)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if n == 301 {
w.Header().Set("Location", "/foobar")
}
w.WriteHeader(n)
io.WriteString(w, http.StatusText(n))
}
func handleHeaders(w http.ResponseWriter, r *http.Request) {
responseJSON, err := json.Marshal(collapseMapVals(r.Header))
if err != nil {
logerror(err.Error())
return
}
io.WriteString(w, string(responseJSON)+"\n")
}
func handlePing(w http.ResponseWriter, r *http.Request) {
response := map[string]bool{"running": true}
responseJSON, _ := json.Marshal(response)
io.WriteString(w, string(responseJSON)+"\n")
}
func main() {
loadRoutes()
server()
}
func loadRoutes() {
routes = make(map[string]func(http.ResponseWriter, *http.Request))
routes["/kubernetes/canary"] = handlePing
routes["/headers"] = handleHeaders
routes["/ping"] = handlePing
routes["/"] = handlePing
}
func (*router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
handler, ok := routes[request.URL.String()]
if ok {
log(request, 200)
handler(writer, request)
} else if strings.HasPrefix(request.URL.String(), httpReturnRequestedCodePath) {
// handle "/httpCode/XXX" - return requested httpCode to the client
handleHTTPCode(writer, request)
} else {
log(request, 404)
http.Error(writer, "404 page not found", 404)
}
}
func server() {
server := http.Server{
Addr: ":" + port,
Handler: &router{},
}
loginfo("starting up server on port " + port)
server.ListenAndServe()
}
// http.Request.Header is a map where the values are arrays of strings,
// and it's nicer to look at output where there's a single key/value
func collapseMapVals(input map[string][]string) map[string]string {
result := map[string]string{}
for key, value := range input {
result[key] = value[0]
}
return result
}
type requestLog struct {
Timestamp string
HTTPCode int
RequestPath string
RemoteHost string
Headers map[string]string
}
func loginfo(msg string) {
fmt.Fprintf(os.Stderr, "{\"info\":\"%s\"}\n", msg)
}
func logerror(msg string) {
fmt.Fprintf(os.Stderr, "{\"error\":\"%s\"}\n", msg)
}
func log(r *http.Request, statusCode int) {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
logline := requestLog{
Timestamp: time.Now().Format(time.RFC3339),
HTTPCode: statusCode,
RequestPath: r.URL.Path,
RemoteHost: ip,
Headers: collapseMapVals(r.Header),
}
json, err := json.Marshal(logline)
if err != nil {
logerror(err.Error())
return
}
fmt.Println(string(json))
}