-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
367 lines (294 loc) · 8.42 KB
/
functions.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
package main
import (
bytes2 "bytes"
"context"
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/utils"
"github.com/gofiber/keyauth/v2"
"github.com/gofiber/swagger"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/joeycumines/go-dotnotation/dotnotation"
"github.com/pelletier/go-toml"
"github.com/pelletier/go-toml/query"
"io"
"log"
"net/http"
"os"
"sync"
"time"
)
var conn *pgxpool.Pool
var config *toml.Tree
func handleServer() {
app := fiber.New(fiber.Config{
ErrorHandler: formErrorMessage,
})
app.Use(logger.New(logger.Config{
Format: config.Get("api.logger.format").(string),
TimeFormat: config.Get("api.logger.time_format").(string),
TimeZone: config.Get("api.logger.timezone").(string),
}))
app.Use(recover.New())
app.Use(cors.New(cors.Config{
AllowOrigins: config.GetArray("api.cors.allow_origins").(string),
AllowHeaders: config.GetArray("api.cors.allow_headers").(string),
}))
app.Get("/docs/*", swagger.HandlerDefault)
api := app.Group("/api")
v1 := api.Group("/v1")
v1.Use(keyauth.New(keyauth.Config{
KeyLookup: config.Get("api.auth.header_key").(string),
ErrorHandler: formErrorMessage,
Validator: validateAuthToken,
}))
v1.Post("/guilds", postGuildCountRoute)
v1.Get("/guilds", getGuildCountRoute)
v1.Get("/services", getBotListServicesRoute)
v1.Get("/services/:service", getSingleBotListServiceRoute)
port := os.Getenv("API_PORT")
log.Fatal(app.Listen(fmt.Sprintf(":%s", port)))
}
func loadDatabase() {
dbpool, connErr := pgxpool.Connect(context.Background(), os.Getenv("POSTGRES_URL"))
if connErr != nil {
log.Fatal(connErr)
}
conn = dbpool
fmt.Println("PostgreSQL database connected!")
}
func loadConfig() {
doc, err := toml.LoadFile("config.toml")
if err != nil {
log.Fatal(err)
}
config = doc
fmt.Println("Services config file loaded!")
}
func formJsonBody(data interface{}, success bool) ResponseHTTP {
return ResponseHTTP{
Data: data,
Success: success,
Nonce: time.Now().UnixMilli(),
}
}
func formErrorMessage(ctx *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
message := "A server side error has occurred."
if e, ok := err.(*fiber.Error); ok {
code = e.Code
message = e.Message
} else if err.Error() != "" {
message = err.Error()
}
return ctx.Status(code).JSON(formJsonBody(
fiber.Map{
"code": code,
"message": message,
},
false,
))
}
func fetchStats(httpClient *http.Client, config BotListServiceConfig) (*BotListServiceResponse, error) {
token := getServiceToken(config.ShortName)
req, err := http.NewRequest("GET", config.GetStatsUrl, nil)
if err != nil {
return &BotListServiceResponse{
ShortName: config.ShortName,
Url: config.Url,
GuildCount: 0,
Error: true,
}, nil
}
req.Header.Set("Authorization", token)
resp, respErr := httpClient.Do(req)
if respErr != nil {
return &BotListServiceResponse{
ShortName: config.ShortName,
Url: config.Url,
GuildCount: 0,
Error: true,
}, nil
}
defer resp.Body.Close()
body, bodyErr := io.ReadAll(resp.Body)
if bodyErr != nil {
return &BotListServiceResponse{
ShortName: config.ShortName,
Url: config.Url,
GuildCount: 0,
Error: true,
}, nil
}
var bodyData interface{}
bodyDataErr := json.Unmarshal(body, &bodyData)
if bodyDataErr != nil {
return &BotListServiceResponse{
ShortName: config.ShortName,
Url: config.Url,
GuildCount: 0,
Error: true,
}, nil
}
var BotListAccessor dotnotation.Accessor
guildCount, gcErr := BotListAccessor.Get(bodyData, config.Accessor)
if gcErr != nil {
return nil, gcErr
}
return &BotListServiceResponse{
ShortName: config.ShortName,
Url: config.Url,
GuildCount: int64(guildCount.(float64)),
}, nil
}
func postStatsToBotList(httpClient *http.Client, service BotListServiceConfig, guildCount int64, shardCount int64) error {
token := getServiceToken(service.ShortName)
var data = fiber.Map{service.Key: guildCount}
if service.ShortName == "botsgg" {
withShardCount := &data
*withShardCount = fiber.Map{service.Key: guildCount, "shardCount": shardCount}
}
jsonData, jsonErr := json.Marshal(data)
if jsonErr != nil {
return jsonErr
}
req, err := http.NewRequest("POST", service.PostStatsUrl, bytes2.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("Authorization", token)
req.Header.Set("Content-Type", "application/json")
resp, respErr := httpClient.Do(req)
if respErr != nil {
return respErr
}
var res fiber.Map
decErr := json.NewDecoder(resp.Body).Decode(&res)
if decErr != nil {
return decErr
}
return nil
}
func postStatsToBotLists(guildCount int64, shardCount int64) []error {
wg := sync.WaitGroup{}
locker := sync.Mutex{}
var errors []error
configs := getActiveServices()
client := &http.Client{Timeout: time.Second * 30}
for _, config := range configs {
wg.Add(1)
go func(c BotListServiceConfig) {
defer wg.Done()
err := postStatsToBotList(client, c, guildCount, shardCount)
if err != nil {
errors = append(errors, err)
return
}
locker.Lock()
defer locker.Unlock()
return
}(getServiceConfig(config))
}
wg.Wait()
return errors
}
func fetchBotListServiceData() ([]BotListServiceResponse, []error) {
wg := sync.WaitGroup{}
locker := sync.Mutex{}
var responses []BotListServiceResponse
var errors []error
configs := getActiveServices()
client := &http.Client{Timeout: time.Second * 30}
for _, config := range configs {
wg.Add(1)
go func(c BotListServiceConfig) {
defer wg.Done()
data, err := fetchStats(client, c)
if err != nil {
errors = append(errors, err)
return
}
locker.Lock()
defer locker.Unlock()
responses = append(responses, *data)
return
}(getServiceConfig(config))
}
wg.Wait()
return responses, errors
}
func getServiceConfig(service string) BotListServiceConfig {
return BotListServiceConfig{
ShortName: config.Get(fmt.Sprintf("services.%s.short_name", service)).(string),
LongName: config.Get(fmt.Sprintf("services.%s.long_name", service)).(string),
Url: config.Get(fmt.Sprintf("services.%s.url", service)).(string),
GetStatsUrl: config.Get(fmt.Sprintf("services.%s.get_stats_url", service)).(string),
PostStatsUrl: config.Get(fmt.Sprintf("services.%s.post_stats_url", service)).(string),
Accessor: config.Get(fmt.Sprintf("services.%s.accessor", service)).(string),
Key: config.Get(fmt.Sprintf("services.%s.key", service)).(string),
Enabled: config.Get(fmt.Sprintf("services.%s.enabled", service)).(bool),
}
}
func getVersion() string {
return config.Get("version").(string)
}
func getServiceToken(service string) string {
return os.Getenv(fmt.Sprintf("SERVICES_%s_TOKEN", utils.ToUpper(service)))
}
func execQuery(query string, args ...interface{}) (pgconn.CommandTag, error) {
return conn.Exec(context.Background(), query, args...)
}
func queryRow(query string, args ...interface{}) error {
return conn.QueryRow(context.Background(), query).Scan(args...)
}
func validateGuildCount(guild GuildCountRequestBody) []*ErrorResponse {
var errors []*ErrorResponse
validate := validator.New()
err := validate.Struct(guild)
if err != nil {
for _, err := range err.(validator.ValidationErrors) {
var element ErrorResponse
element.FailedField = err.StructNamespace()
element.Tag = err.Tag()
element.Value = err.Param()
errors = append(errors, &element)
}
}
return errors
}
func handleBotListErrors(ctx *fiber.Ctx, errors []error) error {
var data []interface{}
for _, err := range errors {
data = append(data, err)
}
ctx.Status(fiber.StatusInternalServerError)
return ctx.JSON(formJsonBody(data, false))
}
func validateAuthToken(_ *fiber.Ctx, token string) (bool, error) {
tk := os.Getenv("API_TOKEN")
if token != tk {
return false, nil
}
return true, nil
}
func getActiveServices() []string {
var services []string
q, _ := query.Compile("$.services[?(active)].short_name")
q.SetFilter("active", func(node interface{}) bool {
if tree, ok := node.(*toml.Tree); ok {
return tree.Get("enabled").(bool) == true
}
return false
})
results := q.Execute(config)
for _, service := range results.Values() {
services = append(services, service.(string))
}
return services
}