-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
152 lines (129 loc) · 4.04 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
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
// Copyright (C) 2015 TF2Stadium
// Use of this source code is governed by the GPLv3
// that can be found in the COPYING file.
package main
import (
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"github.com/sirupsen/logrus"
"github.com/TF2Stadium/Helen/config"
"github.com/TF2Stadium/Helen/controllers"
chelpers "github.com/TF2Stadium/Helen/controllers/controllerhelpers"
"github.com/TF2Stadium/Helen/controllers/socket"
"github.com/TF2Stadium/Helen/database"
"github.com/TF2Stadium/Helen/database/migrations"
"github.com/TF2Stadium/Helen/helpers"
_ "github.com/TF2Stadium/Helen/helpers/authority" // to register authority types
_ "github.com/TF2Stadium/Helen/internal/pprof" // to setup expvars
"github.com/TF2Stadium/Helen/internal/version"
"github.com/TF2Stadium/Helen/models/chat"
"github.com/TF2Stadium/Helen/models/event"
"github.com/TF2Stadium/Helen/models/lobby"
"github.com/TF2Stadium/Helen/models/lobby_settings"
"github.com/TF2Stadium/Helen/models/rpc"
"github.com/TF2Stadium/Helen/routes"
socketServer "github.com/TF2Stadium/Helen/routes/socket"
"github.com/evalphobia/logrus_sentry"
"github.com/rs/cors"
)
var (
flagGen = flag.Bool("genkey", false, "write a 32bit key for encrypting cookies the given file, and exit")
docPrint = flag.Bool("printdoc", false, "print the docs for environment variables, and exit.")
dbMaxopen = flag.Int("db-maxopen", 80, "maximum number of open database connections allowed.")
)
func main() {
flag.Parse()
if *flagGen {
key := make([]byte, 64)
_, err := rand.Read(key)
if err != nil {
logrus.Fatal(err)
}
base64Key := base64.StdEncoding.EncodeToString(key)
fmt.Println(base64Key)
return
}
if *docPrint {
config.PrintConfigDoc()
os.Exit(0)
}
if helpers.Raven != nil {
hook, err := logrus_sentry.NewWithClientSentryHook(helpers.Raven, []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
})
if err == nil {
logrus.AddHook(hook)
} else {
logrus.Fatal(err)
}
}
logrus.Debug("Commit: ", version.GitCommit)
logrus.Debug("Branch: ", version.GitBranch)
logrus.Debug("Build date: ", version.BuildDate)
controllers.InitTemplates()
//models.ReadServers()
err := os.Mkdir(config.Constants.DemosFolder, 0755)
if err != nil && !os.IsExist(err) {
logrus.Fatal(err)
}
if config.Constants.ProfilerAddr != "" {
go http.ListenAndServe(config.Constants.ProfilerAddr, nil)
logrus.Info("Running Profiler at ", config.Constants.ProfilerAddr)
}
database.Init()
database.DB.DB().SetMaxOpenConns(*dbMaxopen)
migrations.Do()
helpers.ConnectAMQP()
event.StartListening()
helpers.InitGeoIPDB()
err = lobbySettings.LoadLobbySettingsFromFile("assets/lobbySettingsData.json")
if err != nil {
logrus.Fatal(err)
}
lobby.CreateLocks()
rpc.ConnectRPC(helpers.AMQPConn)
lobby.RestoreServemeChecks()
//go models.TFTVStreamStatusUpdater()
if config.Constants.SteamIDWhitelist != "" {
go chelpers.WhitelistListener()
}
mux := http.NewServeMux()
routes.SetupHTTP(mux)
socket.RegisterHandlers()
corsHandler := cors.New(cors.Options{
AllowedOrigins: config.Constants.AllowedOrigins,
AllowedMethods: []string{"GET", "POST", "DELETE", "OPTIONS"},
AllowCredentials: true,
}).Handler(mux)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGKILL)
go func() {
<-sig
shutdown()
os.Exit(0)
}()
logrus.Info("Serving on ", config.Constants.ListenAddress)
logrus.Fatal(http.ListenAndServe(config.Constants.ListenAddress, corsHandler))
}
func shutdown() {
logrus.Info("Received SIGINT/SIGTERM")
chat.SendNotification(`Backend will be going down for a while for an update, click on "Reconnect" to reconnect to TF2Stadium`, 0)
logrus.Info("waiting for GlobalWait")
helpers.GlobalWait.Wait()
logrus.Info("waiting for socket requests to complete.")
socketServer.Wait()
logrus.Info("closing all active websocket connections")
socketServer.AuthServer.Close()
logrus.Info("stopping event listener")
event.StopListening()
}