-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
251 lines (220 loc) · 6.11 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
package main
/*
* Copyright 2014 Albert P. Tobey <atobey@datastax.com> @AlTobey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* main.go: http server application for cassandra-summit-cfp-review
*
*/
import (
"encoding/json"
"fmt"
"github.com/gocql/gocql"
"github.com/gorilla/mux"
"log"
"net/http"
"time"
)
func RootHandler(w http.ResponseWriter, r *http.Request) {
// check for auth but ignore the result: this will initialize
// the cookie on page load
checkAuth(w, r, false)
http.ServeFile(w, r, "./public/index.html")
}
func AbstractsHandler(w http.ResponseWriter, r *http.Request) {
if !checkAuth(w, r, false) {
return
}
a := Abstract{}
dec := json.NewDecoder(r.Body)
err := dec.Decode(&a)
switch r.Method {
case "GET":
alist, err := ListAbstracts(cass)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to list abstracts: %s", err), 500)
return
}
jsonOut(w, r, alist)
return
case "PUT":
if err != nil {
log.Printf("AbstractsHandler/PUT invalid json data: %s", err)
http.Error(w, fmt.Sprintf("AbstractsHandler/PUT invalid json data: %s", err), 500)
return
}
a.Id = gocql.TimeUUID()
a.Created = time.Now()
case "PATCH":
if err != nil {
log.Printf("AbstractsHandler/PATCH invalid json data: %s", err)
http.Error(w, fmt.Sprintf("AbstractsHandler/PATCH invalid json data: %s", err), 500)
return
}
default:
http.Error(w, fmt.Sprintf("method '%s' not implemented", r.Method), 500)
return
}
// GET returns early, anything else requires admin privs
if !checkAuth(w, r, true) {
return
}
// bare minimum input checking
if a.Title == "" || a.Body == "" || len(a.Authors) == 0 {
log.Printf("AbstractsHandler required field missing")
http.Error(w, "required field missing", 500)
return
}
err = a.Save(cass)
if err != nil {
log.Printf("AbstractsHandler/%s a.Save() failed: %s", r.Method, err)
http.Error(w, fmt.Sprintf("AbstractsHandler/%s a.Save() failed: %s", r.Method, err), 500)
return
}
jsonOut(w, r, a)
}
func GetAbstractHandler(w http.ResponseWriter, r *http.Request) {
if !checkAuth(w, r, false) {
return
}
vars := mux.Vars(r)
id, err := gocql.ParseUUID(vars["id"])
if err != nil {
http.Error(w, fmt.Sprintf("could not parse uuid: '%s'", err), 500)
return
}
a, _ := FetchAbstract(cass, id)
jsonOut(w, r, a)
}
func DeleteAbstractHandler(w http.ResponseWriter, r *http.Request) {
if !checkAuth(w, r, true) {
return
}
vars := mux.Vars(r)
id, err := gocql.ParseUUID(vars["id"])
if err != nil {
http.Error(w, fmt.Sprintf("could not parse uuid: '%s'", err), 500)
return
}
err = DeleteAbstract(cass, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func ScoreUpdateHandler(w http.ResponseWriter, r *http.Request) {
if !checkAuth(w, r, false) {
return
}
scores := make(ScoreUpdates, 7)
dec := json.NewDecoder(r.Body)
err := dec.Decode(&scores)
if err != nil {
log.Printf("invalid score update json: %s\n", err)
http.Error(w, fmt.Sprintf("invalid score update json: %s", err), 500)
return
}
err = scores.Save(cass)
if err != nil {
log.Printf("score update failed: %s\n", err)
http.Error(w, fmt.Sprintf("score update failed: %s", err), 500)
return
}
jsonOut(w, r, scores)
}
func CommentsHandler(w http.ResponseWriter, r *http.Request) {
if !checkAuth(w, r, false) {
return
}
c := Comment{}
if r.Method == "GET" {
vars := mux.Vars(r)
absid, err := gocql.ParseUUID(vars["abstract_id"])
if err != nil {
log.Printf("Could not parse uuid '%s': %s\n", vars["id"], err)
http.Error(w, fmt.Sprintf("could not parse uuid: '%s'", err), 500)
return
}
clist, err := ListComments(cass, absid)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to list comments: %s", err), 500)
return
}
jsonOut(w, r, clist)
return
} else if r.Method == "PUT" || r.Method == "PATCH" {
dec := json.NewDecoder(r.Body)
err := dec.Decode(&c)
if err != nil {
log.Printf("CommentsHandler/%s invalid json data: %s", r.Method, err)
http.Error(w, fmt.Sprintf("CommentsHandler/%s invalid json data: %s", r.Method, err), 500)
return
}
} else {
http.Error(w, fmt.Sprintf("method '%s' not implemented", r.Method), 500)
return
}
if r.Method == "PUT" {
c.Id = gocql.TimeUUID()
}
// bare minimum input checking
if c.Email == "" || c.Body == "" {
log.Printf("CommentsHandler/%s required field missing\n", r.Method)
http.Error(w, "required field missing", 500)
return
}
err := c.Save(cass)
if err != nil {
http.Error(w, fmt.Sprintf("CommentHandler c.Save() failed: %s", err), 500)
return
}
jsonOut(w, r, c)
}
func AdminsHandler(w http.ResponseWriter, r *http.Request) {
admins, err := fetchAdmins()
if err != nil {
http.Error(w, fmt.Sprintf("AdminsHandler failed: %s", err), 500)
return
}
jsonOut(w, r, admins)
}
// returns the email string if authenticated (via persona), it won't
// be there at all if the user didn't authenticate
func checkAuth(w http.ResponseWriter, r *http.Request, adminOnly bool) bool {
sess, err := store.Get(r, sessCookie)
if err != nil {
log.Printf("failed to read cookie: %s\n", err)
return false
}
if sess.IsNew {
log.Printf("Saving session ID '%s' to Cassandra.\n", sess.ID)
sess.Save(r, w)
}
if sess.Values["email"] != nil {
email := sess.Values["email"].(string)
//log.Printf("sess.Values[email]: '%s'\n", sess.Values["email"])
if email != "" {
// authentication is successful, now check for admin status if that is requested
if adminOnly {
isAdmin, err := checkIfAdmin(email)
if err != nil {
log.Printf("admin check failed: %s\n", err)
return false
}
return isAdmin
}
return true
}
}
return false
}