forked from Ellie42/Erupe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (120 loc) · 3.48 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/Andoryuuta/Erupe/config"
"github.com/Andoryuuta/Erupe/server/channelserver"
"github.com/Andoryuuta/Erupe/server/entranceserver"
"github.com/Andoryuuta/Erupe/server/launcherserver"
"github.com/Andoryuuta/Erupe/server/signserver"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"go.uber.org/zap"
)
// Temporary DB auto clean on startup for quick development & testing.
func cleanDB(db *sqlx.DB) {
_ = db.MustExec("DELETE FROM guild_characters")
_ = db.MustExec("DELETE FROM guilds")
_ = db.MustExec("DELETE FROM characters")
_ = db.MustExec("DELETE FROM sign_sessions")
_ = db.MustExec("DELETE FROM users")
}
func main() {
zapLogger, _ := zap.NewDevelopment()
defer zapLogger.Sync()
logger := zapLogger.Named("main")
logger.Info("Starting Erupe")
// Load the configuration.
erupeConfig, err := config.LoadConfig()
if err != nil {
logger.Fatal("Failed to load config", zap.Error(err))
}
// Create the postgres DB pool.
connectString := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname= %s sslmode=disable",
erupeConfig.Database.Host,
erupeConfig.Database.Port,
erupeConfig.Database.User,
erupeConfig.Database.Password,
erupeConfig.Database.Database,
)
db, err := sqlx.Open("postgres", connectString)
if err != nil {
logger.Fatal("Failed to open sql database", zap.Error(err))
}
// Test the DB connection.
err = db.Ping()
if err != nil {
logger.Fatal("Failed to ping database", zap.Error(err))
}
logger.Info("Connected to database")
// Clean the DB if the option is on.
if erupeConfig.DevMode && erupeConfig.DevModeOptions.CleanDB {
logger.Info("Cleaning DB")
cleanDB(db)
logger.Info("Done cleaning DB")
}
// Now start our server(s).
// Launcher HTTP server.
launcherServer := launcherserver.NewServer(
&launcherserver.Config{
Logger: logger.Named("launcher"),
ErupeConfig: erupeConfig,
DB: db,
UseOriginalLauncherFiles: erupeConfig.Launcher.UseOriginalLauncherFiles,
})
err = launcherServer.Start()
if err != nil {
logger.Fatal("Failed to start launcher server", zap.Error(err))
}
logger.Info("Started launcher server.")
// Entrance server.
entranceServer := entranceserver.NewServer(
&entranceserver.Config{
Logger: logger.Named("entrance"),
ErupeConfig: erupeConfig,
DB: db,
})
err = entranceServer.Start()
if err != nil {
logger.Fatal("Failed to start entrance server", zap.Error(err))
}
logger.Info("Started entrance server.")
// Sign server.
signServer := signserver.NewServer(
&signserver.Config{
Logger: logger.Named("sign"),
ErupeConfig: erupeConfig,
DB: db,
})
err = signServer.Start()
if err != nil {
logger.Fatal("Failed to start sign server", zap.Error(err))
}
logger.Info("Started sign server.")
// Channel Server
channelServer := channelserver.NewServer(
&channelserver.Config{
Logger: logger.Named("channel"),
ErupeConfig: erupeConfig,
DB: db,
})
err = channelServer.Start()
if err != nil {
logger.Fatal("Failed to start channel server", zap.Error(err))
}
logger.Info("Started channel server.")
// Wait for exit or interrupt with ctrl+C.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
logger.Info("Trying to shutdown gracefully.")
channelServer.Shutdown()
signServer.Shutdown()
entranceServer.Shutdown()
launcherServer.Shutdown()
time.Sleep(1 * time.Second)
}