-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
70 lines (66 loc) · 1.71 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
package main
import (
"fmt"
"github.com/discordextremelist/api/entities"
"github.com/discordextremelist/api/routes"
"github.com/discordextremelist/api/util"
"github.com/getsentry/sentry-go"
"github.com/go-chi/chi"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"time"
)
var (
check = []string{"ADDR", "PORT", "REDIS_PASSWORD", "REDIS_DB", "MONGO_URL", "MONGO_DB"}
)
func init() {
for _, v := range os.Args {
if v == "--dev" {
util.Dev = true
}
}
log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.TextFormatter{ForceColors: true, FullTimestamp: true})
_ = godotenv.Load()
for i := 0; i < len(check); i++ {
if _, ok := os.LookupEnv(check[i]); !ok {
log.Fatalf("Required environmental variable '%s' doesn't exist!", check[i])
}
}
}
func main() {
util.InitSentry()
defer sentry.Flush(2 * time.Second)
if !util.Dev {
util.BuildClient()
err := util.FindKubernetesNode()
if err != nil {
log.Errorf("Failed to get the node this pod is on: %v", err)
} else {
log.Infof("Currently on node: %s", util.Node)
}
}
util.Database.OpenRedisConnection()
util.Database.OpenMongoConnection()
if util.Dev {
entities.PopulateDevCache()
}
util.Router = chi.NewRouter()
util.Router.Use(util.RealIP)
util.Router.Use(entities.RequestLogger)
util.Router.NotFound(entities.NotFound)
util.Router.Use(entities.TokenValidator)
routes.InitGeneralRoutes()
routes.InitBotRoutes()
routes.InitUserRoutes()
routes.InitServerRoutes()
routes.InitTemplateRoutes()
routes.InitDebugRoutes()
ip := os.Getenv("ADDR")
port := os.Getenv("PORT")
serve := fmt.Sprintf("%s:%s", ip, port)
log.Infof("Starting to serve at: %s", serve)
log.Fatal(http.ListenAndServe(serve, util.Router))
}