This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
367 lines (300 loc) · 8.11 KB
/
api.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
package splenda
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
)
// An API instance hosts the Splenda HTTP API.
type API interface {
Serve(addr string) error
}
// NewAPI creates a new API.
func NewAPI(auth *Auth, impl *Impl) API {
return &api{auth, impl}
}
type api struct {
auth *Auth
impl *Impl
}
// Serve serves the API on the given endpoint.
func (a *api) Serve(port string) error {
http.HandleFunc("/", a.Root)
http.HandleFunc("/assets/", a.Asset)
http.HandleFunc("/signup", a.Signup)
http.HandleFunc("/login", a.Login)
http.HandleFunc("/logout", a.Logout)
http.HandleFunc("/games/", a.Game)
http.HandleFunc("/api/users", a.UsersAPI)
http.HandleFunc("/api/login", a.LoginAPI)
http.HandleFunc("/api/games", a.GamesAPI)
http.HandleFunc("/api/games/", a.GameAPI)
return http.ListenAndServe(port, nil)
}
// Authorize checks if the request contains a valid session id and, if so,
// extracts and returns the userID.
func (a *api) authorize(req *http.Request) (string, error) {
sid, err := req.Cookie("sid")
if err != nil {
return "", err
}
return a.auth.Authorize(sid.Value)
}
// UsersAPI dispatches GET|POST /api/users to the appropriate handler.
func (a *api) UsersAPI(res http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
a.ListUsersAPI(res, req)
case http.MethodPost:
a.NewUserAPI(res, req)
default:
res.WriteHeader(405)
}
}
// ListUsersAPI handles GET /api/users, listing registered users.
func (a *api) ListUsersAPI(res http.ResponseWriter, req *http.Request) {
_, err := a.authorize(req)
if err != nil {
res.WriteHeader(401)
return
}
users, err := a.auth.ListUsers()
if err != nil {
// TODO: better error handling?
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
write(UserList{users}, res)
}
// NewUserAPI handles POST /api/users, registering a new user.
func (a *api) NewUserAPI(res http.ResponseWriter, req *http.Request) {
login := Login{}
if err := unmarshal(req.Body, &login); err != nil {
// TODO: better error handling?
res.WriteHeader(400)
res.Write([]byte(err.Error() + "\n"))
return
}
err := a.auth.NewUser(login.ID, login.Password)
if err != nil {
// TODO: better error handling?
res.WriteHeader(400)
res.Write([]byte(err.Error() + "\n"))
return
}
res.WriteHeader(201)
}
// LoginAPI handles POST /api/login, programmatically logging in to the service.
func (a *api) LoginAPI(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
res.WriteHeader(405)
return
}
login := Login{}
if err := unmarshal(req.Body, &login); err != nil {
res.WriteHeader(400)
return
}
sid, err := a.auth.Login(login.ID, login.Password)
if err != nil {
// TODO: better error handling?
res.WriteHeader(400)
res.Write([]byte(err.Error() + "\n"))
return
}
write(SID{sid}, res)
}
// GamesAPI dispatches GET|POST /api/games to the right handler.
func (a *api) GamesAPI(res http.ResponseWriter, req *http.Request) {
id, err := a.authorize(req)
if err != nil {
res.WriteHeader(401)
return
}
switch req.Method {
case http.MethodGet:
a.ListGamesAPI(id, res, req)
case http.MethodPost:
a.NewGameAPI(id, res, req)
default:
res.WriteHeader(405)
}
}
// ListGamesAPI handles GET /api/games, listing the user's currently-running games.
func (a *api) ListGamesAPI(userID string, res http.ResponseWriter, req *http.Request) {
games, err := a.impl.ListGames(userID)
if err != nil {
// TODO: better error handling?
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
write(GameList{games}, res)
}
// NewGameAPI handles POST /api/games, starting a new game.
func (a *api) NewGameAPI(userID string, res http.ResponseWriter, req *http.Request) {
game := GameSummary{}
if err := unmarshal(req.Body, &game); err != nil {
res.WriteHeader(400)
res.Write([]byte(err.Error() + "\n"))
return
}
if game.ID != "" {
res.WriteHeader(400)
res.Write([]byte("cannot set id\n"))
return
}
id, err := a.impl.NewGame(userID, game.Players)
if err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
game.ID = id
write(game, res)
}
// GameAPI dispatches GET|POST|DELETE /api/games/<id> to the right handler.
func (a *api) GameAPI(res http.ResponseWriter, req *http.Request) {
userID, err := a.authorize(req)
if err != nil {
res.WriteHeader(401)
return
}
path := strings.TrimPrefix(req.URL.Path, "/api/games/")
switch req.Method {
case http.MethodGet:
a.GetGameAPI(userID, path, res, req)
case http.MethodDelete:
a.DeleteGameAPI(userID, path, res, req)
case http.MethodPost:
a.MoveAPI(userID, path, res, req)
default:
res.WriteHeader(405)
return
}
}
// GetGameAPI handles GET /api/games/<id>, getting the current state of a particular game.
func (a *api) GetGameAPI(userID string, path string, res http.ResponseWriter, req *http.Request) {
gameID := path
ts := req.URL.Query().Get("ts")
game, err := a.impl.GetGame(gameID, userID, ts)
if err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
write(game, res)
}
// DeleteGameAPI handles DELETE /api/games/<id>, deleting a game.
func (a *api) DeleteGameAPI(userID string, path string, res http.ResponseWriter, req *http.Request) {
gameID := path
if err := a.impl.DeleteGame(gameID, userID); err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
res.WriteHeader(204)
}
// MoveAPI handles POST /api/games/<id>/<move>, performing a move.
func (a *api) MoveAPI(userID string, path string, res http.ResponseWriter, req *http.Request) {
idx := strings.IndexByte(path, '/')
if idx == -1 {
res.WriteHeader(405)
return
}
gameID := path[:idx]
trailer := path[idx+1:]
switch trailer {
case "take3":
a.Take3API(gameID, userID, res, req)
case "take2":
a.Take2API(gameID, userID, res, req)
case "reserve":
a.ReserveAPI(gameID, userID, res, req)
case "buy":
a.BuyAPI(gameID, userID, res, req)
default:
res.WriteHeader(404)
}
}
// Take3API handles POST /api/games/<id>/take3, taking three coins from the table.
func (a *api) Take3API(gameID string, userID string, res http.ResponseWriter, req *http.Request) {
move := Take3{}
if err := unmarshal(req.Body, &move); err != nil {
res.WriteHeader(400)
return
}
ts, err := a.impl.Take3(gameID, userID, move.Colors)
if err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
write(TS{ts}, res)
}
// Take2API handles POST /games/<id>/take2, taking two coins from the table.
func (a *api) Take2API(gameID string, userID string, res http.ResponseWriter, req *http.Request) {
move := Take2{}
if err := unmarshal(req.Body, &move); err != nil {
res.WriteHeader(400)
return
}
ts, err := a.impl.Take2(gameID, userID, move.Color)
if err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
write(TS{ts}, res)
}
// ReserveAPI handles POST /games/<id>/reserve, reserving a card.
func (a *api) ReserveAPI(gameID string, userID string, res http.ResponseWriter, req *http.Request) {
move := Buy{}
if err := unmarshal(req.Body, &move); err != nil {
res.WriteHeader(400)
return
}
ts, err := a.impl.Reserve(gameID, userID, move.Tier, move.Index)
if err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
write(TS{ts}, res)
}
// BuyAPI handles POST /games/<id>/buy, buying a card.
func (a *api) BuyAPI(gameID string, userID string, res http.ResponseWriter, req *http.Request) {
move := Buy{}
if err := unmarshal(req.Body, &move); err != nil {
res.WriteHeader(400)
return
}
ts, err := a.impl.Buy(gameID, userID, move.Tier, move.Index)
if err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
write(TS{ts}, res)
}
func write(d interface{}, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
marshal(d, w)
}
func marshal(src interface{}, dst io.Writer) {
bs, err := json.Marshal(src)
if err != nil {
panic(err)
}
dst.Write(bs)
}
func unmarshal(src io.Reader, dst interface{}) error {
bs, err := ioutil.ReadAll(src)
if err != nil {
return err
}
return json.Unmarshal(bs, dst)
}