-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_handlers.go
368 lines (314 loc) · 8.75 KB
/
http_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
package main
// this file contains implementation of HTTP handlers - REST API
import (
"encoding/json"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"log"
"net/http"
"strconv"
"time"
)
var (
// TODO move this secret to environment
jwtSecret = []byte("secret")
service Service
radio *Radio
)
func NewHTTPRouter(_service Service, _radio *Radio) *echo.Echo {
service = _service
radio = _radio
r := echo.New()
r.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))
// router := echo.New()
router := r.Group("/api")
router.File("/test_subscribe", "index.html")
router.GET("/isLeader", tellIfLeader)
router.GET("/health", healthCheckHandler)
router.POST("/login", loginHandler)
router.GET("/subscribe", subscribeToUpdatesHandler)
// this is placed here because it uses cookies instead of JWT
router.GET("/link/by_me", linksByMeHandler)
linkGroup := router.Group("/link")
linkGroup.Use(middleware.JWT(jwtSecret))
{
linkGroup.GET("/:id", linkByIdHandler)
// linkGroup.GET("/by_me", linksByMeHandler)
linkGroup.POST("/new", newLinkHandler)
linkGroup.POST("/upvote", upvoteLinkHandler)
linkGroup.POST("/downvote", downvoteLinkHandler)
}
radioGroup := router.Group("/radio")
radioGroup.Use(middleware.JWT(jwtSecret))
{
radioGroup.GET("/now_playing", radioGetNowPlayingHandler)
radioGroup.GET("/queue", radioGetQueueHandler)
}
// return router
return r
}
func subscribeToUpdatesHandler(c echo.Context) error {
cookie, err := c.Cookie("userid")
if err != nil {
return c.String(http.StatusUnauthorized, "Missing cookie userid")
}
userID := cookie.Value
var hookType HookType = HookType(c.QueryParam("hooktype"))
if len(hookType) == 0 {
return c.String(http.StatusBadRequest, "Missing hookType param")
}
if !IsValidHookType(hookType) {
return c.String(http.StatusBadRequest, "Invalid hook type")
}
var w http.ResponseWriter = c.Response().Writer
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported", http.StatusInternalServerError)
}
id, hookChan := radio.RegisterHook(hookType)
defer radio.DeregisterHook(hookType, id)
log.Println("client connected with id", id, "for", hookType)
notifyCloseChan := w.(http.CloseNotifier).CloseNotify()
go func() {
<-notifyCloseChan
radio.DeregisterHook(hookType, id)
log.Println("HTTP connection closed")
}()
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
for {
state, open := <-hookChan
log.Println("sse handler got ", state)
if !open {
break
}
if hookType == queueHook {
links := state.([]Link)
votes := service.GetVotesForUser(links, userID)
for i, l := range links {
if vote, ok := votes[l.LinkID]; ok {
links[i].MyVote = vote
} else {
links[i].MyVote = 0
}
}
state = echo.Map{
"links": links,
"votes": votes,
}
}
msg, err := json.Marshal(state)
if err != nil {
log.Panicln("Error while marshalling", err)
}
msgstr := string(msg)
// data: is required and \n\n as well
fmt.Fprint(w, "data: ", msgstr, "\n\n")
f.Flush()
}
log.Println("Finished HTTP request for", id)
return nil
}
func linkByIdHandler(c echo.Context) error {
lid, _ := strconv.Atoi(c.Param("id"))
l, _ := service.GetLinkByID(int64(lid))
return c.JSON(http.StatusOK, l)
}
func linksByMeHandler(c echo.Context) error {
// this one is ReST based
// userID := getUserIDFromContext(c)
// links := service.GetLinksByUser(userID)
// return c.JSON(http.StatusOK, links)
// this implementation uses SSE
cookie, err := c.Cookie("userid")
if err != nil {
return c.String(http.StatusUnauthorized, "Missing cookie userid")
}
userID := cookie.Value
var w http.ResponseWriter = c.Response().Writer
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported", http.StatusInternalServerError)
}
log.Println("client connected with for linksbyme")
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
ticker := time.NewTicker(time.Second * 2)
notifyCloseChan := w.(http.CloseNotifier).CloseNotify()
go func() {
<-notifyCloseChan
ticker.Stop()
log.Println("HTTP connection closed for linksbyme")
}()
for range ticker.C {
links := service.GetLinksByUser(userID)
msg, err := json.Marshal(links)
if err != nil {
log.Panicln("Error while marshalling", err)
}
fmt.Fprint(w, "data: ", string(msg), "\n\n")
f.Flush()
}
log.Println("Finished HTTP request for my submissions")
return nil
}
func loginHandler(c echo.Context) error {
u := User{}
if err := c.Bind(&u); err != nil {
return err
}
// I shouldn't be doing this
u.UserID = c.FormValue("user_id")
service.CreateOrUpdateUser(u)
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["user_id"] = u.UserID
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
t, err := token.SignedString(jwtSecret)
if err != nil {
return err
}
return c.JSON(http.StatusOK, echo.Map{
"token": t,
})
}
func newLinkHandler(c echo.Context) error {
form := struct {
URL string `form:"url" validate:"required"`
DedicatedTo string `form:"dedicated_to"`
}{}
if err := c.Bind(&form); err != nil {
return c.String(http.StatusBadRequest, "Missing form data")
}
log.Println(form.URL, "is the url")
userID := getUserIDFromContext(c)
link, err := service.SubmitLink(form.URL, userID, form.DedicatedTo)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{
"message": err.Error(),
})
}
return c.JSON(http.StatusOK, link)
}
func downvoteLinkHandler(c echo.Context) error {
form := struct {
LinkID int64 `form:"link_id" validate:"required"`
}{}
if err := c.Bind(&form); err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{
"message": "Missing link_id",
})
}
userID := getUserIDFromContext(c)
service.Vote(form.LinkID, userID, -1)
return c.JSON(http.StatusOK, echo.Map{
"message": "Done",
})
}
func upvoteLinkHandler(c echo.Context) error {
form := struct {
LinkID int64 `form:"link_id" validate:"required"`
}{}
if err := c.Bind(&form); err != nil || form.LinkID == 0 {
return c.JSON(http.StatusBadRequest, echo.Map{
"message": "Missing link_id",
})
}
userID := getUserIDFromContext(c)
log.Println(form.LinkID)
service.Vote(form.LinkID, userID, +1)
return c.JSON(http.StatusOK, echo.Map{
"message": "Done",
})
}
func healthCheckHandler(c echo.Context) error {
message := c.QueryParam("message")
service.Test(message)
return c.String(http.StatusOK, "I am up and running!")
}
func getUserIDFromContext(c echo.Context) string {
return c.Get("user").(*jwt.Token).Claims.(jwt.MapClaims)["user_id"].(string)
}
// TODO implement this using SSE
func radioGetNowPlayingHandler(c echo.Context) error {
if radio.nowPlaying == nil {
return c.JSON(http.StatusOK, echo.Map{
"state": "idle",
"link": nil,
"player_time": 0,
})
}
userID := getUserIDFromContext(c)
links := []Link{*radio.nowPlaying}
votes := service.GetVotesForUser(links, userID)
linkIDs := make([]int64, len(links))
for i, l := range links {
linkIDs[i] = l.LinkID
}
totalVotes := service.GetTotalVoteForLinks(linkIDs)
for i, l := range links {
if score, ok := totalVotes[l.LinkID]; ok {
links[i].TotalVotes = score
} else {
links[i].TotalVotes = 0
}
}
var myVote int64
if len(votes) == 0 {
myVote = 0
} else {
myVote = votes[radio.nowPlaying.LinkID]
}
user := service.GetUserByID(radio.nowPlaying.SubmittedBy)
return c.JSON(http.StatusOK, echo.Map{
"state": "running",
"link": radio.nowPlaying,
"submitted_by": echo.Map{
"firstname": user.FirstName,
"lastname": user.LastName,
},
"my_vote": myVote,
"player_time": radio.playerCurTimeSec,
})
}
// TODO implement this using SSE
func radioGetQueueHandler(c echo.Context) error {
links := radio.queue
userID := getUserIDFromContext(c)
votes := service.GetVotesForUser(links, userID)
for i, l := range links {
if vote, ok := votes[l.LinkID]; ok {
links[i].MyVote = vote
} else {
links[i].MyVote = 0
}
}
return c.JSON(http.StatusOK, echo.Map{
"links": links,
"votes": votes,
})
}
func tellIfLeader(c echo.Context) error {
switch radio.radioType {
case masterRadio:
return c.JSON(http.StatusExpectationFailed, echo.Map{
"message": "I am not the leader",
})
// return c.JSON(http.StatusOK, echo.Map{
// "message": "I am the leader",
// })
case peerRadio:
return c.JSON(http.StatusExpectationFailed, echo.Map{
"message": "I am not the leader",
})
}
return nil
}