generated from BattlesnakeOfficial/starter-snake-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (118 loc) · 3.63 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
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
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/newrelic/go-agent/v3/integrations/logcontext-v2/logWriter"
"github.com/newrelic/go-agent/v3/newrelic"
api "github.com/nosnaws/tiam/battlesnake"
instru "github.com/nosnaws/tiam/instrumentation"
"strconv"
)
const ServerID = "nosnaws/lancer"
func recordLatency(app *newrelic.Application, state api.GameState) {
latency, err := strconv.ParseFloat(state.You.Latency, 64)
if err == nil {
app.RecordCustomMetric("lastTurnLatency", latency)
}
}
// HTTP Handlers
func HandleIndex(w http.ResponseWriter, r *http.Request) {
response := info()
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
if err != nil {
log.Printf("ERROR: Failed to encode info response, %s", err)
}
}
func HandleStart(w http.ResponseWriter, r *http.Request) {
state := api.GameState{}
err := json.NewDecoder(r.Body).Decode(&state)
if err != nil {
log.Printf("ERROR: Failed to decode start json, %s", err)
return
}
start(state)
// Nothing to respond with here
}
func HandleMove(app *newrelic.Application) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
duration, err := time.ParseDuration("350ms")
if err != nil {
panic("could not parse duration")
}
ctx, cancel := context.WithTimeout(r.Context(), duration)
defer cancel()
state := api.GameState{}
err = json.NewDecoder(r.Body).Decode(&state)
if err != nil {
log.Printf("ERROR: Failed to decode move json, %s", err)
return
}
txn := newrelic.FromContext(r.Context())
instru.GetCustomAttributes(txn, state)
//recordLatency(app, state)
txn.AddAttribute("lastTurnLatency", state.You.Latency)
response := move(ctx, state)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
log.Printf("ERROR: Failed to encode move response, %s", err)
return
}
}
}
func HandleEnd(w http.ResponseWriter, r *http.Request) {
state := api.GameState{}
err := json.NewDecoder(r.Body).Decode(&state)
if err != nil {
log.Printf("ERROR: Failed to decode end json, %s", err)
return
}
txn := newrelic.FromContext(r.Context())
instru.GetCustomAttributesEnd(txn, state)
txn.AddAttribute("lastTurnLatency", state.You.Latency)
end(state)
// Nothing to respond with here
}
// Middleware
func withServerID(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", ServerID)
next(w, r)
}
}
func logRequest(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next(w, r)
log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start))
}
}
// Main Entrypoint
func main() {
log.Println("Version", runtime.Version())
log.Println("NumCPU", runtime.NumCPU())
log.Println("GOMAXPROCS", runtime.GOMAXPROCS(0))
port := os.Getenv("PORT")
nrLicenseKey := os.Getenv("NEW_RELIC_LICENSE_KEY")
if len(port) == 0 {
port = "8080"
}
app, _ := newrelic.NewApplication(
newrelic.ConfigAppName("Lancer"),
newrelic.ConfigLicense(nrLicenseKey),
)
writer := logWriter.New(os.Stdout, app)
log.SetOutput(writer)
http.HandleFunc(newrelic.WrapHandleFunc(app, "/", withServerID(HandleIndex)))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/start", withServerID(HandleStart)))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/move", withServerID(HandleMove(app))))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/end", withServerID(HandleEnd)))
log.Printf("Starting Battlesnake Server at http://0.0.0.0:%s...\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}