-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (94 loc) · 2.66 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
)
const leaderboard string = "leaderboard"
func (a *app) viewRank(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
zRank := a.Redis.ZRank(leaderboard, id)
user := user{ID: id, Rank: zRank.Val()}
respondWithJSON(w, http.StatusOK, user)
}
func (a *app) viewLeaderboard(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
zRank := a.Redis.ZRank(leaderboard, id)
lower := zRank.Val() - 1
upper := zRank.Val() + 1
zRangeWithScores := a.Redis.ZRangeWithScores(leaderboard, lower, upper)
users := []user{}
for _, data := range zRangeWithScores.Val() {
member, _ := data.Member.(string)
user := user{ID: member, Score: data.Score}
users = append(users, user)
}
respondWithJSON(w, http.StatusOK, users)
}
func (a *app) updateRank(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
newUser := new(user)
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(newUser)
if err != nil {
fmt.Println(err)
}
zAdd := a.Redis.ZAdd(leaderboard, redis.Z{newUser.Score, id})
added, _ := zAdd.Result()
if added == int64(1) {
respondWithJSON(w, http.StatusOK, ``)
}
}
func (a *app) topRanks(w http.ResponseWriter, r *http.Request) {
zRevRangeWithScores := a.Redis.ZRevRangeWithScores(leaderboard, 0, 2)
users := []user{}
for _, data := range zRevRangeWithScores.Val() {
member, _ := data.Member.(string)
user := user{ID: member, Score: data.Score}
users = append(users, user)
}
respondWithJSON(w, http.StatusOK, users)
}
func respondWithJSON(w http.ResponseWriter, code int, user interface{}) {
response, _ := json.Marshal(user)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
type user struct {
ID string `json:"id,omitempty"`
Rank int64 `json:"rank,omitempty"`
Score float64 `json:"score,omitempty"`
}
type app struct {
Router *mux.Router
Redis *redis.Client
}
func (a *app) initialize() {
a.Router = mux.NewRouter()
a.Redis = redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "", // no password set
DB: 0, // use default DB
})
a.initializeRoutes()
}
func (a *app) initializeRoutes() {
a.Router.HandleFunc("/leaderboard/{id}", a.viewLeaderboard).Methods("GET")
a.Router.HandleFunc("/user/{id}", a.viewRank).Methods("GET")
a.Router.HandleFunc("/user/{id}", a.updateRank).Methods("POST")
a.Router.HandleFunc("/topusers", a.topRanks).Methods("GET")
}
func (a *app) run() {
http.ListenAndServe(":8080", a.Router)
}
func main() {
a := app{}
a.initialize()
a.run()
}