-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflags.go
48 lines (46 loc) · 987 Bytes
/
flags.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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func FlagsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Got /flags")
userId, err := userID(r)
if err != nil {
return
}
if r.Method == "GET" {
flags, err := getUserFlags(userId, "uflags")
if err != nil {
w.WriteHeader(500)
return
}
if err := json.NewEncoder(w).Encode(flags); err != nil {
w.WriteHeader(500)
return
}
} else if r.Method == "POST" {
flags := []string{}
// TODO: Restrict # of flags that can be set and limit their size
if err := json.NewDecoder(r.Body).Decode(&flags); err != nil {
w.WriteHeader(500)
return
}
for _, v := range flags {
setUserFlag(userId, "uflags", v)
}
} else if r.Method == "DELETE" {
flags := []string{}
if err := json.NewDecoder(r.Body).Decode(&flags); err != nil {
w.WriteHeader(500)
return
}
for _, v := range flags {
unsetUserFlag(userId, "uflags", v)
}
} else {
w.WriteHeader(500)
return
}
}