-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
179 lines (154 loc) · 4.37 KB
/
main.go
File metadata and controls
179 lines (154 loc) · 4.37 KB
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
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"otpgo/clientagent"
"otpgo/core"
"otpgo/database"
"otpgo/dc"
"otpgo/eventlogger"
"otpgo/luarole"
"otpgo/messagedirector"
"otpgo/stateserver"
"otpgo/util"
"path"
"path/filepath"
"strings"
"github.com/apex/log"
"github.com/carlmjohnson/versioninfo"
"github.com/spf13/pflag"
)
var mainLog *log.Entry
func init() {
log.SetHandler(core.Log)
log.SetLevel(log.DebugLevel)
mainLog = log.WithFields(log.Fields{
"name": "Main",
"modName": "Main",
})
}
func main() {
pflag.Usage = func() {
fmt.Printf(
`Usage: otpgo [options]... [CONFIG_FILE]
OtpGo is an OTP (Online Theme Park) server written in Go.
By default OtpGo looks for a configuration file in the current
working directory as otp.yml. A different config file path
can be specified as a positional argument.
-h, --help Print this help dialog.
-v, --version Print version information.
-L, --log Specify a file to write log messages to.
-l, --loglevel Specify the minimum log level that should be logged;
Error and Fatal levels will always be logged.
`)
os.Exit(1)
}
logfilePtr := pflag.StringP("log", "L", "", "Specify the file to write log messages to.")
loglevelPtr := pflag.StringP("loglevel", "l", "debug", "Specify minimum log level that should be logged.")
versionPtr := pflag.BoolP("version", "v", false, "Show the application version.")
helpPtr := pflag.BoolP("help", "h", false, "Show the application usage.")
pflag.Parse()
if *helpPtr {
pflag.Usage()
os.Exit(1)
}
if *versionPtr {
fmt.Printf(`
OTP (Online Theme Park) server written in Go.
https://github.com/LittleToonCat/OtpGo
(Based on the unfinished AstronGo project by nosyliam)
https://github.com/nosyliam/AstronGo
Revision: %s
`, versioninfo.Revision)
os.Exit(1)
}
if *loglevelPtr != "" {
loglevelChoices := map[string]log.Level{"info": log.InfoLevel, "warning": log.WarnLevel, "error": log.ErrorLevel, "fatal": log.FatalLevel, "debug": log.DebugLevel}
if choice, validChoice := loglevelChoices[*loglevelPtr]; !validChoice {
mainLog.Fatal(fmt.Sprintf("Unknown log-level \"%s\".", *loglevelPtr))
pflag.Usage()
os.Exit(1)
} else {
log.SetLevel(choice)
}
}
if *logfilePtr != "" {
logfile, err := os.OpenFile(*logfilePtr, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
mainLog.Fatal(fmt.Sprintf("Failed to open log file \"%s\".", *logfilePtr))
os.Exit(1)
}
logfile.Truncate(0)
logfile.Seek(0, 0)
defer logfile.Sync()
defer logfile.Close()
handler := core.NewMultiHandler(core.Log, core.NewLogger(logfile))
log.SetHandler(handler)
}
var configPath, configName string
args := pflag.Args()
if len(args) > 0 {
configName = filepath.Base(args[0])
configName = strings.TrimSuffix(configName, path.Ext(configName))
configPath = filepath.Dir(args[0])
} else {
configName = "otp"
configPath = "."
}
mainLog.Info("Loading configuration file...")
if err := core.LoadConfig(configPath, configName); err != nil {
mainLog.Fatal(err.Error())
}
if err := core.LoadDC(); err != nil {
mainLog.Fatal(err.Error())
}
// Start pprof if enabled
if core.Config.Debug.Pprof {
go func() {
err := http.ListenAndServe("localhost:6060", nil)
if err != nil {
mainLog.Error(err.Error())
}
}()
}
eventlogger.StartEventSender(core.Config.General.Eventlogger)
messagedirector.Start()
// Configure UberDOG list
for _, ud := range core.Config.Uberdogs {
class := core.DC.GetClassByName(ud.Class)
// Check if the method returns a NULL pointer
if class == dc.SwigcptrDCClass(0) {
mainLog.Fatalf("For UberDOG %d, class %s does not exist!", ud.ID, ud.Class)
return
}
core.Uberdogs = append(core.Uberdogs, core.Uberdog{
Id: util.Doid_t(ud.ID),
Class: class,
})
}
// Instantiate roles
for _, role := range core.Config.Roles {
switch role.Type {
case "clientagent":
clientagent.NewClientAgent(role)
case "database":
database.NewDatabaseServer(role)
case "dbss":
stateserver.NewDatabaseStateServer(role)
case "eventlogger":
eventlogger.StartEventLogger(role)
case "lua":
luarole.NewLuaRole(role)
case "stateserver":
stateserver.NewStateServer(role)
}
}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
sig := <-c
mainLog.Fatal(fmt.Sprintf("Got %s signal. Aborting...", sig))
os.Exit(1)
}