-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
141 lines (115 loc) · 2.98 KB
/
api.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/kataras/iris/v12"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type API struct {
*iris.Application
db *Database
}
func NewAPI(config *Config) *API {
app := iris.New()
db := connectDatabase(config.MongoURI)
api := &API{app, db}
api.registerErrors()
api.registerRoutes()
return api
}
func (api *API) StartCron() {
go api.db.RemoveOldEntriesJob()
go api.UpdateLeaderboardJob()
}
func (api *API) registerErrors() {
api.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "not found"})
})
}
func (api *API) registerRoutes() {
api.Get("/", func(ctx iris.Context) {
// serve the rendered openapi specification
ctx.JSON(iris.Map{"wys g": "wagwan g"})
})
apiRoot := api.Party("/api", func(ctx iris.Context) {
log.Printf("Request: %s %s", ctx.Method(), ctx.Path())
ctx.Next()
})
v1 := apiRoot.Party("/v1")
v1.Get("/leaderboard", api.GetLeaderboard)
}
func (api *API) GetLeaderboard(ctx iris.Context) {
params := ctx.URLParams()
category, ok := params["category"]
if !ok {
category = "all"
}
var categories []Category
if category == "all" {
categories = AllCategories
} else {
categories = make([]Category, 0)
for _, c := range strings.Split(category, ",") {
category := StringToCategory(c)
switch category {
case CategoryAll:
categories = append(categories, CategoryAll)
break
case CategoryClans:
categories = append(categories, CategoryClans)
case CategoryXP:
categories = append(categories, CategoryXP)
case CategoryHeals:
categories = append(categories, CategoryHeals)
case CategoryRevives:
categories = append(categories, CategoryRevives)
case CategoryVehiclesDestroyed:
categories = append(categories, CategoryVehiclesDestroyed)
case CategoryVehicleRepairs:
categories = append(categories, CategoryVehicleRepairs)
case CategoryKills:
categories = append(categories, CategoryKills)
default:
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(iris.Map{"error": fmt.Sprintf("invalid category: %s", c)})
return
}
}
}
page, ok := params["page"]
if !ok {
page = "1"
}
pageInt, err := strconv.Atoi(page)
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(iris.Map{"error": "invalid page, must be an integer"})
return
}
if pageInt < 1 || pageInt > 249 {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(iris.Map{"error": "invalid page"})
return
}
var cursorID *primitive.ObjectID
cursor := params["cursor"]
if cursor != "" {
cursorObjID, err := primitive.ObjectIDFromHex(cursor)
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(iris.Map{"error": "invalid cursor"})
return
}
cursorID = &cursorObjID
}
lb, err := api.db.GetLeaderboard(categories, cursorID, pageInt)
if err != nil {
log.Println(err)
ctx.StatusCode(iris.StatusInternalServerError)
ctx.JSON(iris.Map{"error": "failed to get leaderboard"})
return
}
ctx.JSON(lb)
}