forked from cyverse-de/user-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bags.go
406 lines (342 loc) · 11.3 KB
/
bags.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 (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/cyverse-de/queries"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
// BagsApp contains the routing and request handling code for bags.
type BagsApp struct {
api *BagsAPI
router *mux.Router
userDomain string
}
// NewBagsApp creates a new BagsApp instance.
func NewBagsApp(db *sql.DB, router *mux.Router, userDomain string) *BagsApp {
bagsApp := &BagsApp{
api: &BagsAPI{
db: db,
},
router: router,
userDomain: userDomain,
}
bagsApp.router.HandleFunc("/bags/", bagsApp.Greeting).Methods(http.MethodGet)
bagsApp.router.HandleFunc("/bags/{username}", bagsApp.HasBags).Methods(http.MethodHead)
bagsApp.router.HandleFunc("/bags/{username}/default", bagsApp.GetDefaultBag).Methods(http.MethodGet)
bagsApp.router.HandleFunc("/bags/{username}/default", bagsApp.UpdateDefaultBag).Methods(http.MethodPost)
bagsApp.router.HandleFunc("/bags/{username}/default", bagsApp.DeleteDefaultBag).Methods(http.MethodDelete)
bagsApp.router.HandleFunc("/bags/{username}", bagsApp.GetBags).Methods(http.MethodGet)
bagsApp.router.HandleFunc("/bags/{username}/{bagID}", bagsApp.GetBag).Methods(http.MethodGet)
bagsApp.router.HandleFunc("/bags/{username}", bagsApp.AddBag).Methods(http.MethodPut)
bagsApp.router.HandleFunc("/bags/{username}/{bagID}", bagsApp.UpdateBag).Methods(http.MethodPost)
bagsApp.router.HandleFunc("/bags/{username}/{bagID}", bagsApp.DeleteBag).Methods(http.MethodDelete)
bagsApp.router.HandleFunc("/bags/{username}", bagsApp.DeleteAllBags).Methods(http.MethodDelete)
return bagsApp
}
// AddUsernameSuffix appends the @iplantcollaborative.org string to the
// username if it's not already there.
func (b *BagsApp) AddUsernameSuffix(username string) string {
var retval string
if !strings.HasSuffix(username, IplantSuffix) {
retval = fmt.Sprintf("%s%s", username, IplantSuffix)
} else {
retval = username
}
return retval
}
// Greeting prints out a greeting for the bags endpoints.
func (b *BagsApp) Greeting(writer http.ResponseWriter, request *http.Request) {
fmt.Fprintf(writer, "Hello from the bags handler")
}
func (b *BagsApp) getUser(vars map[string]string) (string, int, error) {
var (
username string
err error
ok, userExists bool
)
if username, ok = vars["username"]; !ok {
return "", http.StatusBadRequest, errors.New("missing username in the URL")
}
username = b.AddUsernameSuffix(username)
if userExists, err = queries.IsUser(b.api.db, username); err != nil {
return "", http.StatusInternalServerError, fmt.Errorf("error checking for bags %s: %s", username, err)
}
if !userExists {
return "", http.StatusNotFound, fmt.Errorf("user %s does not exist", username)
}
return username, http.StatusOK, nil
}
// GetBags returns a listing of the bags for the user.
func (b *BagsApp) GetBags(writer http.ResponseWriter, request *http.Request) {
var (
username string
bags []BagRecord
err error
status int
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
return
}
if bags, err = b.api.GetBags(username); err != nil {
http.Error(writer, fmt.Sprintf("error getting bags for %s: %s", username, err), http.StatusInternalServerError)
return
}
jsonBytes, err := json.Marshal(map[string][]BagRecord{"bags": bags})
if err != nil {
http.Error(writer, fmt.Sprintf("error JSON encoding result for %s: %s", username, err), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
if _, err = writer.Write(jsonBytes); err != nil {
log.Error(err)
}
}
// GetBag returns a single bag.
func (b *BagsApp) GetBag(writer http.ResponseWriter, request *http.Request) {
var (
username, bagID string
bag BagRecord
err error
ok bool
status int
vars = mux.Vars(request)
jsonBytes []byte
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if bagID, ok = vars["bagID"]; !ok {
badRequest(writer, "missing bagID in the URL")
return
}
if ok, err = b.api.HasBag(username, bagID); err != nil {
badRequest(writer, fmt.Sprintf("error checking database for bag %s for %s: %s", bagID, username, err))
return
}
if !ok {
http.Error(writer, fmt.Sprintf("bag %s not found for user %s", bagID, username), http.StatusNotFound)
return
}
if bag, err = b.api.GetBag(username, bagID); err != nil {
http.Error(writer, fmt.Sprintf("error getting bags for %s: %s", username, err), http.StatusInternalServerError)
return
}
if jsonBytes, err = json.Marshal(bag); err != nil {
http.Error(writer, fmt.Sprintf("error JSON encoding result for %s: %s", username, err), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
if _, err = writer.Write(jsonBytes); err != nil {
log.Error(err)
}
}
// GetDefaultBag will return the default bag for the user, creating a new one and setting it as the default if no default is
// already set.
func (b *BagsApp) GetDefaultBag(writer http.ResponseWriter, request *http.Request) {
var (
username string
bag BagRecord
err error
status int
jsonBytes []byte
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if bag, err = b.api.GetDefaultBag(username); err != nil {
http.Error(writer, fmt.Sprintf("error getting default bag for %s: %s", username, err), http.StatusInternalServerError)
return
}
if jsonBytes, err = json.Marshal(bag); err != nil {
http.Error(writer, fmt.Sprintf("error JSON encoding result for %s: %s", username, err), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
if _, err = writer.Write(jsonBytes); err != nil {
log.Error(err)
}
}
// AddBag adds an additional bag to the list for the user.
func (b *BagsApp) AddBag(writer http.ResponseWriter, request *http.Request) {
var (
username, bagID string
bag BagRecord
err error
body []byte
retval []byte
status int
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if body, err = ioutil.ReadAll(request.Body); err != nil {
errored(writer, fmt.Sprintf("error reading body: %s", err))
return
}
if err = json.Unmarshal(body, &bag); err != nil {
badRequest(writer, fmt.Sprintf("failed to JSON decode body: %s", err))
return
}
if bagID, err = b.api.AddBag(username, string(body)); err != nil {
errored(writer, fmt.Sprintf("failed to add bag for %s: %s", username, err))
return
}
if retval, err = json.Marshal(map[string]string{"id": bagID}); err != nil {
errored(writer, fmt.Sprintf("failed to JSON encode response body: %s", err))
return
}
writer.Header().Set("Content-Type", "application/json")
if _, err = writer.Write(retval); err != nil {
log.Error(err)
}
}
// UpdateBag updates the indicated bag.
func (b *BagsApp) UpdateBag(writer http.ResponseWriter, request *http.Request) {
var (
username, bagID string
bag BagRecord
err error
ok bool
body []byte
status int
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if bagID, ok = vars["bagID"]; !ok {
badRequest(writer, "missing bagID in the URL")
return
}
if ok, err = b.api.HasBag(username, bagID); err != nil {
badRequest(writer, fmt.Sprintf("error checking database for bag %s for %s: %s", bagID, username, err))
return
}
if !ok {
http.Error(writer, fmt.Sprintf("bag %s not found for user %s", bagID, username), http.StatusNotFound)
return
}
if body, err = ioutil.ReadAll(request.Body); err != nil {
errored(writer, fmt.Sprintf("error reading body: %s", err))
return
}
if err = json.Unmarshal(body, &bag); err != nil {
errored(writer, fmt.Sprintf("failed to JSON decode body: %s", err))
return
}
if err = b.api.UpdateBag(username, bagID, string(body)); err != nil {
errored(writer, fmt.Sprintf("error updating bag for user %s: %s", username, err))
return
}
}
// UpdateDefaultBag sets new contents for the user's default bag.
func (b *BagsApp) UpdateDefaultBag(writer http.ResponseWriter, request *http.Request) {
var (
username string
bag BagRecord
err error
body []byte
status int
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if body, err = ioutil.ReadAll(request.Body); err != nil {
errored(writer, fmt.Sprintf("error reading body: %s", err))
return
}
if err = json.Unmarshal(body, &bag); err != nil {
errored(writer, fmt.Sprintf("failed to JSON decode body: %s", err))
return
}
if err = b.api.UpdateDefaultBag(username, string(body)); err != nil {
errored(writer, fmt.Sprintf("error updating default bag for user %s: %s", username, err))
return
}
}
// DeleteBag deletes a single bag for a user.
func (b *BagsApp) DeleteBag(writer http.ResponseWriter, request *http.Request) {
var (
username, bagID string
err error
ok bool
status int
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if bagID, ok = vars["bagID"]; !ok {
badRequest(writer, "missing bagID in the URL")
return
}
if err = b.api.DeleteBag(username, bagID); err != nil {
errored(writer, fmt.Sprintf("error deleting bag for user %s: %s", username, err))
return
}
}
// DeleteDefaultBag deletes the default bag for the user from the database.
func (b *BagsApp) DeleteDefaultBag(writer http.ResponseWriter, request *http.Request) {
var (
username string
err error
status int
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if err = b.api.DeleteDefaultBag(username); err != nil {
errored(writer, fmt.Sprintf("error deleting default bag for user %s: %s", username, err))
return
}
}
// DeleteAllBags deletes all bags for a user
func (b *BagsApp) DeleteAllBags(writer http.ResponseWriter, request *http.Request) {
var (
username string
err error
status int
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if err = b.api.DeleteAllBags(username); err != nil {
errored(writer, fmt.Sprintf("error deleting bag for user %s: %s", username, err))
return
}
}
// HasBags returns true if the user has at least a single bag in the database.
func (b *BagsApp) HasBags(writer http.ResponseWriter, request *http.Request) {
var (
username string
err error
hasBags bool
status int
vars = mux.Vars(request)
)
if username, status, err = b.getUser(vars); err != nil {
http.Error(writer, err.Error(), status)
}
if hasBags, err = b.api.HasBags(username); err != nil {
errored(writer, fmt.Sprintf("error looking for bags for %s: %s", username, err))
return
}
if !hasBags {
writer.WriteHeader(http.StatusNotFound)
} else {
writer.WriteHeader(http.StatusOK)
}
}