-
Notifications
You must be signed in to change notification settings - Fork 3
/
goHandlers.go
88 lines (84 loc) · 1.78 KB
/
goHandlers.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
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
"strconv"
"video-feed/kafka"
"video-feed/redis"
)
func VideoHandler(w http.ResponseWriter, r *http.Request){
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, http.StatusText(405), 405)
return
}
id := mux.Vars(r)["id"]
if id == "" {
http.Error(w, http.StatusText(400), 400)
return
}
vd, err := redis.VideoDisplay(id)
if err == redis.VideoNoError {
http.Error(w, http.StatusText(400),400)
return
} else if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
_, err = fmt.Fprintf(w, "Id->%s\n" +
"Title->%s\n"+
"category->%s\n"+
"likes->%d\n",id, vd.Title, vd.Category, vd.Likes)
if err !=nil{
http.Error(w,http.StatusText(500),500)
}
}
func LikeHandler(w http.ResponseWriter, r *http.Request){
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, http.StatusText(405), 405)
return
}
id := mux.Vars(r)["id"]
if id == "" {
http.Error(w, http.StatusText(400), 400)
return
}
go kafka.Produce("likes",id)
}
func PopularHandler(w http.ResponseWriter, r *http.Request){
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, http.StatusText(405), 405)
return
}
x := mux.Vars(r)["num"]
if x == "" {
x = "3"
}
xInt,err := strconv.Atoi(x)
if err != nil {
http.Error(w, http.StatusText(400), 400)
return
}
vds, err := redis.GetPopular(xInt)
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
c:=0
for i, vd := range vds{
if vd == nil{
break
}
_, err = fmt.Fprintf(w, "No->%d\n" +
"Title->%s\n"+
"category->%s\n"+
"likes->%d\n\n",i,vd.Title, vd.Category, vd.Likes)
c = c + 1
}
if c == 0{
fmt.Fprintln(w,"No video liked yet")
}
}