forked from TF2Stadium/Helen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (119 loc) · 3.43 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
// 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"
"time"
"gopkg.in/tylerb/graceful.v1"
"github.com/DSchalla/go-pid"
"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/models"
"github.com/TF2Stadium/Helen/models/event"
"github.com/TF2Stadium/Helen/routes"
socketServer "github.com/TF2Stadium/Helen/routes/socket"
"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.")
)
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)
}
controllers.InitTemplates()
config.SetupConstants()
helpers.SetServemeContext()
//models.ReadServers()
chelpers.SetupJWTSigning()
if config.Constants.ProfilerAddr != "" {
go graceful.Run(config.Constants.ProfilerAddr, 1*time.Second, nil)
logrus.Info("Running Profiler at ", config.Constants.ProfilerAddr)
}
helpers.ConnectAMQP()
event.StartListening()
database.Init()
migrations.Do()
err := models.LoadLobbySettingsFromFile("assets/lobbySettingsData.json")
if err != nil {
logrus.Fatal(err)
}
models.CreateLocks()
models.ConnectRPC()
models.DeleteUnusedServerRecords()
//go models.TFTVStreamStatusUpdater()
helpers.InitGeoIPDB()
if config.Constants.SteamIDWhitelist != "" {
go chelpers.WhitelistListener()
}
mux := http.NewServeMux()
routes.SetupHTTP(mux)
socket.RegisterHandlers()
corsHandler := cors.New(cors.Options{
AllowedOrigins: []string{config.Constants.CORSWhitelist},
AllowedMethods: []string{"GET", "POST", "DELETE"},
AllowCredentials: true,
}).Handler(mux)
pid := &pid.Instance{}
if pid.Create() == nil {
defer pid.Remove()
}
// start the server
server := graceful.Server{
Timeout: 10 * time.Second,
Server: &http.Server{
Addr: config.Constants.ListenAddress,
Handler: corsHandler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
},
ShutdownInitiated: func() {
logrus.Info("Received SIGINT/SIGTERM, closing RPC")
logrus.Info("waiting for socket requests to complete.")
socketServer.Wait()
logrus.Info("waiting for GlobalWait")
helpers.GlobalWait.Wait()
logrus.Info("closing all active websocket connections")
socketServer.AuthServer.Close()
logrus.Info("stopping event listener")
event.StopListening()
},
}
//start health checks
if config.Constants.HealthChecks {
controllers.StartHealthCheck()
}
logrus.Info("Serving on ", config.Constants.ListenAddress)
err = server.ListenAndServe()
if err != nil {
logrus.Fatal(err)
}
}