-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
181 lines (154 loc) · 5.13 KB
/
http.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"encoding/json"
"fmt"
"html"
"leaderboard/config"
"leaderboard/security"
"log"
"net/http"
"strconv"
"time"
"github.com/google/uuid"
"github.com/spf13/viper"
)
type playerValues struct {
Score string `json:"score"`
Name string `json:"name"`
Checksum string `json:"checksum"`
}
type contextKey string
const (
correlationIDContextKey contextKey = "correlationID"
)
// scorePostHandler will accept a request to post a score.
func scorePostHandler(w http.ResponseWriter, r *http.Request) {
uuid := uuid.New().String()
var input playerValues
inputType := viper.GetString(config.InputType)
switch inputType {
case "json":
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
logMessage(r.Context(), fmt.Sprintf("couldn't decode request: %s", err.Error()))
return
}
case "form":
input.Score = r.FormValue("score")
input.Name = r.FormValue("name")
input.Checksum = r.FormValue("x")
}
score := input.Score
name := input.Name
checksum := input.Checksum
logMessage(r.Context(), fmt.Sprintf("Incoming score post request input_type=%s score=%s name=%s checksum=%s", inputType, score, name, checksum))
maxLength := viper.GetInt(config.MaxNameLength)
if maxLength > 0 {
if len(name) > maxLength {
name = name[:maxLength]
logMessage(r.Context(), fmt.Sprintf("name truncated"))
}
}
//validate request via security
err := security.ValidateRequestParams(score, name, checksum)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
logMessage(r.Context(), fmt.Sprintf("failed to validate request params: %v", err.Error()))
return
}
points, err := strconv.Atoi(score)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
logMessage(r.Context(), fmt.Sprintf("error converting to string: %v", err))
return
}
err = logScore(name, points, uuid)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
logMessage(r.Context(), fmt.Sprintf("error logging score: %v", err))
return
}
myRank, allResults, err := showScores(uuid)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
logMessage(r.Context(), fmt.Sprintf("error gathering scores: %v", err))
return
}
var res = struct {
AllScores []RankedResult `json:"allScores"`
MyRank int `json:"rank"`
}{
allResults,
myRank,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(res)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
logMessage(r.Context(), fmt.Sprintf("couldn't encode response: %s", err.Error()))
return
}
}
// loggerMiddleware will log information about teh current request as it comes in and as it finishes.
func loggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
corrID := uuid.New()
r = r.WithContext(context.WithValue(r.Context(), correlationIDContextKey, corrID))
logMessage(r.Context(), fmt.Sprintf("Handling Request path=%s method=%s useragent=%s", r.URL.Path, r.Method, r.UserAgent()))
next.ServeHTTP(w, r)
logMessage(r.Context(), fmt.Sprintf("Request Complete duration=%d", time.Since(start)))
})
}
// logMessage will automatically handle the correlation id and message formatting.
func logMessage(ctx context.Context, message string) {
corrID := ctx.Value(correlationIDContextKey)
log.Printf("correlationID=%s msg=%s", corrID, message)
}
// route the incoming call to a json view or webview based on query param.
func getRouter(w http.ResponseWriter, r *http.Request) {
if shouldShowWebView(r) {
printScoreTable(w, r)
} else {
fetchAll(w, r)
}
}
// return whatever the query params is set to or the value set in config
func shouldShowWebView(r *http.Request) bool {
qp := r.URL.Query()
if _, ok := qp["webview"]; ok {
val, _ := strconv.ParseBool(qp.Get("webview"))
return val
}
return viper.GetBool(config.WebviewEnabled)
}
// show an html view of the scores
func printScoreTable(w http.ResponseWriter, r *http.Request) {
ranked := getAllRankedScoresFromTree()
ret := "<table><th>Place</th><th>Name</th><th>Score</th>"
for _, score := range ranked {
ret += fmt.Sprintf("<tr><td>%v</td><td>%v</td><td>%v</td></tr>", score.Place, html.EscapeString(score.Name), score.Score)
}
ret += "</table>"
_, err := w.Write([]byte(ret))
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
logMessage(r.Context(), fmt.Sprintf("couldn't write response: %s", err.Error()))
return
}
}
func fetchAll(w http.ResponseWriter, r *http.Request) {
//fmt.Println("fetching all scores")
ranked := getAllRankedScoresFromTree()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(ranked)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
logMessage(r.Context(), fmt.Sprintf("couldn't encode response: %s", err.Error()))
return
}
}