-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
406 lines (343 loc) · 14 KB
/
handlers.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"regexp"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/satori/go.uuid"
)
// WebError is a custom error type for reporting bad events when making an HTTP request
// swagger:model
type WebError struct {
Error error
Message string
Code int
}
// simplify error reporting in web handlers by making our own type that handles WebError return values
type errorHandler func(http.ResponseWriter, *http.Request) *WebError
// ... make anything of errorHandler type satisy the http.Handler interface requirements
func (fn errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e := fn(w, r); e != nil { // note that e is *webError, not os.Error.
http.Error(w, e.Message, e.Code)
}
}
// SlashHandler is a slim handler to present some canned text for humans to read
func SlashHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("GOTAK!\n"))
}
// NewGame will generate a new board with a specified size and return the UUID by which it will be known throughout its short, happy life.
func (env *DBenv) NewGame(w http.ResponseWriter, r *http.Request) *WebError {
player, err := env.authUser(r)
if err != nil {
return &WebError{err, fmt.Sprintf("problem authenticating user: %v", err), http.StatusUnprocessableEntity}
}
// pull the boardSize out of the URL itself, e.g. /newgame/4, /newgame/6
vars := mux.Vars(r)
var boardSize int
if boardSize, err = strconv.Atoi(vars["boardSize"]); err != nil {
return &WebError{fmt.Errorf("could not understand requested board size: %v", vars["boardSize"]), fmt.Sprintf("could not understand requested board size: %v", vars["boardSize"]), http.StatusBadRequest}
}
newGame, err := MakeGame(boardSize)
if err != nil {
return &WebError{fmt.Errorf("could not create requested board size: %v", err), fmt.Sprintf("could not create requested board: %v", err), http.StatusInternalServerError}
}
// optional URL parameter to indicate the game's open to anyone. Future use, I suspect.
isPublic, _ := regexp.MatchString("^(?i)true|yes$", r.FormValue("public"))
newGame.GameOwner = player.Username
newGame.IsPublic = isPublic
// stash the new game in the db
if err := env.db.StoreTakGame(newGame); err != nil {
return &WebError{errors.New("problem storing new game"), "problem storing new game", http.StatusInternalServerError}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
gamePayload, _ := json.Marshal(newGame)
w.Write([]byte(gamePayload))
return nil
}
// ShowGame takes a given UUID, looks up the game (if it exists) and returns the current grid
func (env *DBenv) ShowGame(w http.ResponseWriter, r *http.Request) *WebError {
player, err := env.authUser(r)
if err != nil {
return &WebError{err, fmt.Sprintf("problem authenticating user: %v", err), http.StatusUnprocessableEntity}
}
vars := mux.Vars(r)
var (
gameID uuid.UUID
requestedGame *TakGame
)
if gameID, err = uuid.FromString(vars["gameID"]); err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "requested game ID '%v' not understood.", gameID)
}
if requestedGame, err = env.db.RetrieveTakGame(gameID); err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "requested game '%v' not found.", gameID)
}
if requestedGame.CanShow(player) {
// optional URL parameter to just show the stack tops.
showTops, _ := regexp.MatchString("^(?i)true|yes$", r.FormValue("showtops"))
var gamePayload []byte
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if showTops {
topView := requestedGame.DrawStackTops()
gamePayload, _ = json.Marshal(topView)
} else {
fmt.Printf("before: %+v\n\n", requestedGame)
gamePayload, _ = json.Marshal(requestedGame)
fmt.Printf("after: %+v\n\n", requestedGame)
}
w.Write([]byte(gamePayload))
} else {
return &WebError{errors.New("Not allowed to display game"), "Not allowed to display game", http.StatusForbidden}
}
return nil
}
// Action will accept a JSON action for a particular game, determine whether it's a placement or movement, execute it if rules allow, and then return the updated grid.
func (env *DBenv) Action(w http.ResponseWriter, r *http.Request) *WebError {
player, err := env.authUser(r)
if err != nil {
return &WebError{err, fmt.Sprintf("problem authenticating user: %v", err), http.StatusUnprocessableEntity}
}
// get the gameID from the URL path
vars := mux.Vars(r)
gameID, err := uuid.FromString(vars["gameID"])
if err != nil {
return &WebError{err, fmt.Sprintf("Problem with game ID: %v", err), http.StatusNotAcceptable}
}
// fetch out and validate that we've got a game by that ID
requestedGame, err := env.db.RetrieveTakGame(gameID)
if err != nil {
return &WebError{err, "No such game found", http.StatusNotFound}
}
if !requestedGame.PlayersTurn(player) {
return &WebError{errors.New("Not your turn"), "Not this players turn", http.StatusBadRequest}
}
// read in only up to 1MB of data from the client. Come on, now.
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
log.Println(err)
}
var (
placement Placement
movement Movement
)
if vars["action"] == "place" {
if unmarshalError := json.Unmarshal(body, &placement); unmarshalError != nil {
return &WebError{unmarshalError, "Problem decoding JSON", http.StatusUnprocessableEntity}
}
// json.Unmarshal will parse valid but inapplicable JSON into an empty struct. Catch that.
if ((placement.Piece) == Piece{} || (placement.Coords) == "") {
return &WebError{errors.New("Missing piece and/or coordinates"), "Placement is missing piece and/or coordinates", http.StatusUnprocessableEntity}
}
// Place that Piece!
if placementErr := requestedGame.PlacePiece(placement); placementErr != nil {
return &WebError{err, fmt.Sprintf("problem placing piece at %v: %v", placement.Coords, placementErr), 409}
}
if requestedGame.StartTime.IsZero() {
requestedGame.StartTime = time.Now()
}
} else if vars["action"] == "move" {
if unmarshalError := json.Unmarshal(body, &movement); unmarshalError != nil {
return &WebError{unmarshalError, "Problem decoding JSON", http.StatusUnprocessableEntity}
}
// json.Unmarshal will parse valid but inapplicable JSON into an empty struct. Catch that.
if (movement.Coords) == "" || (movement.Direction == "") {
return &WebError{errors.New("Missing coordinates and/or direction"), "Missing coordinates and/or direction", http.StatusUnprocessableEntity}
}
// Move that Stack!
if movementErr := requestedGame.MoveStack(movement); movementErr != nil {
return &WebError{err, fmt.Sprintf("%v: %v", movementErr, movement.Coords), 409}
}
// set StartTime, if it hasn't been set
if requestedGame.StartTime.IsZero() {
requestedGame.StartTime = time.Now()
}
}
// store the updated game back in the DB
if err = env.db.StoreTakGame(requestedGame); err != nil {
return &WebError{err, fmt.Sprintf("storage problem: %v", err), http.StatusInternalServerError}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
gamePayload, _ := json.Marshal(requestedGame)
w.Write([]byte(gamePayload))
return nil
}
// Login checks credentials before issuing a JWT auth token
func (env *DBenv) Login(w http.ResponseWriter, r *http.Request) *WebError {
var (
player TakPlayer
)
// read in only up to 1MB of data from the client. Come on, now.
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
log.Println(err)
}
if unmarshalError := json.Unmarshal(body, &player); unmarshalError != nil {
return &WebError{unmarshalError, "Problem decoding JSON", http.StatusUnprocessableEntity}
}
// json.Unmarshal will parse valid but inapplicable JSON into an empty struct. Catch that.
if player.Username == "" || player.Password == "" {
log.WithFields(log.Fields{"username": player.Username, "password": player.Password}).Debug("login problem")
return &WebError{errors.New("Missing username or password"), "Missing player username or password", http.StatusUnprocessableEntity}
}
if !env.db.PlayerExists(player.Username) {
return &WebError{fmt.Errorf("No player named '%v' found", player.Username), fmt.Sprintf("No player named '%v' found", player.Username), http.StatusUnprocessableEntity}
}
dbPlayer, _ := env.db.RetrievePlayer(player.Username)
if !VerifyPassword(player.Password, string(dbPlayer.passwordHash)) {
return &WebError{errors.New("Incorrect password"), "incorrect password", http.StatusBadRequest}
}
token := generateJWT(&player, "successfully logged in")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(token)
return nil
}
// Register handles new players
func (env *DBenv) Register(w http.ResponseWriter, r *http.Request) *WebError {
// swagger:route POST /register Register
//
// registers a new user
//
// Consumes:
// - application/json
// Produces:
// - application/json
// Responses:
// 200: TakJWT
// 422: "WebError"
// 500: WebError
var newPlayer TakPlayer
// read in only up to 1MB of data from the client. Come on, now.
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
log.Println(err)
}
if unmarshalError := json.Unmarshal(body, &newPlayer); unmarshalError != nil {
return &WebError{unmarshalError, "Problem decoding JSON", http.StatusUnprocessableEntity}
}
// json.Unmarshal will parse valid but inapplicable JSON into an empty struct. Catch that.
if newPlayer.Username == "" || newPlayer.Password == "" {
log.WithFields(log.Fields{"username": newPlayer.Username, "password": newPlayer.Password}).Debug("register problem")
return &WebError{errors.New("Missing new player username or password"), "Missing new player username or password", http.StatusUnprocessableEntity}
}
if env.db.PlayerExists(newPlayer.Username) {
return &WebError{fmt.Errorf("new player username %v conflicts with existing username", newPlayer.Username), fmt.Sprintf("new player username '%v' conflicts with existing username", newPlayer.Username), http.StatusUnprocessableEntity}
}
// every player gets a unique uuid
newPlayer.PlayerID = uuid.NewV4()
newPlayer.passwordHash = HashPassword(newPlayer.Password)
if err := env.db.StorePlayer(&newPlayer); err != nil {
return &WebError{err, fmt.Sprintf("storage problem: %v", err), http.StatusInternalServerError}
}
tokenBytes := generateJWT(&newPlayer, fmt.Sprintf("new player %v successfully created", newPlayer.Username))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(tokenBytes)
return nil
}
// TakeSeat gives an open seat in a game to a requesting player
func (env *DBenv) TakeSeat(w http.ResponseWriter, r *http.Request) *WebError {
// swagger:route GET /takeseat/{gameID} TakeSeat
// for an authenticated user, allocates them an open seat on a specified game
//
// Consumes:
//
// Produces:
// - application/json
// Responses:
// 200: TakGame
// 404: WebError
// 406: WebError
// 422: WebError
// 500: WebError
player, err := env.authUser(r)
if err != nil {
return &WebError{err, fmt.Sprintf("problem authenticating user: %v", err), http.StatusUnprocessableEntity}
}
// get the gameID from the URL path
vars := mux.Vars(r)
gameID, err := uuid.FromString(vars["gameID"])
if err != nil {
return &WebError{err, "Problem with game ID", http.StatusNotAcceptable}
}
// fetch out and validate that we've got a game by that ID
requestedGame, err := env.db.RetrieveTakGame(gameID)
if err != nil {
return &WebError{err, "No such game found", http.StatusNotFound}
}
switch {
case requestedGame.WhitePlayer == player.Username || requestedGame.BlackPlayer == player.Username:
return &WebError{errors.New("already seated at this game"), "already seated at this game", http.StatusConflict} // both seats are open
case requestedGame.WhitePlayer == "" && requestedGame.BlackPlayer == "":
// flip a coin to see which of the open seats you get.
r := rand.New(rand.NewSource(time.Now().UnixNano()))
if r.Intn(2) == 0 {
requestedGame.BlackPlayer = player.Username
} else {
requestedGame.WhitePlayer = player.Username
}
case requestedGame.WhitePlayer != "" && requestedGame.BlackPlayer != "":
// both seats are occupied
return &WebError{errors.New("both seats already taken"), "both seats already taken", http.StatusConflict}
case requestedGame.BlackPlayer == "":
requestedGame.BlackPlayer = player.Username
case requestedGame.WhitePlayer == "":
requestedGame.WhitePlayer = player.Username
}
// store the updated game back in the DB
if err = env.db.StoreTakGame(requestedGame); err != nil {
return &WebError{err, fmt.Sprintf("storage problem: %v", err), http.StatusInternalServerError}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
gamePayload, _ := json.Marshal(requestedGame)
w.Write([]byte(gamePayload))
return nil
}
// GameIDParam is a uuid key specifying one TakGame
// swagger:parameters TakeSeat ShowGame Action
type GameIDParam struct {
// gameID is useful
// in: path
GameID string `json:"gameID"`
}
// MarshalJSON is here to allow saner presentation via JSON methods
// func (tg *TakGame) MarshalJSON() ([]byte, error) {
// // copy the game so as to avoid recursively calling MarshalJSON
// // thanks https://ashleyd.ws/custom-json-marshalling-in-golang/
// type GameAlias TakGame
//
// return json.Marshal(&struct {
// GameBoard GameBoard `json:"gameBoard"`
// *GameAlias
// }{
// GameBoard: MakeGameBoardCartesian(&tg.GameBoard),
// GameAlias: (*GameAlias)(tg),
// })
// }
// MakeGameBoardCartesian should produce a gameboard rotated 90 degrees for more intuitive json marshaling
func MakeGameBoardCartesian(gb *GameBoard) GameBoard {
boardSize := len(*gb)
rotatedBoard := make(GameBoard, boardSize)
for i := range rotatedBoard {
rotatedBoard[i] = make([]Stack, boardSize)
}
for y := 0; y < boardSize; y++ {
for x := 0; x < boardSize; x++ {
rotatedBoard[y][(boardSize-x)-1] = (*gb)[x][y]
}
}
return rotatedBoard
}