-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
284 lines (224 loc) · 7.42 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"context"
"encoding/json"
"errors"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"syscall"
"time"
"fyne.io/systray"
"github.com/kirsle/configdir"
"github.com/leighmacdonald/bd/platform"
"github.com/leighmacdonald/bd/rules"
"github.com/leighmacdonald/bd/store"
_ "modernc.org/sqlite"
)
var (
// Build info embedded at build time.
version = "master" //nolint:gochecknoglobals
commit = "latest" //nolint:gochecknoglobals
date = "n/a" //nolint:gochecknoglobals
)
func createRulesEngine(settings userSettings) *rules.Engine {
rulesEngine := rules.New()
if settings.RunMode != ModeTest { //nolint:nestif
// Try and load our existing custom players
if platform.Exists(settings.LocalPlayerListPath()) {
input, errInput := os.Open(settings.LocalPlayerListPath())
if errInput != nil {
slog.Error("Failed to open local player list", errAttr(errInput))
} else {
var localPlayersList rules.PlayerListSchema
if errRead := json.NewDecoder(input).Decode(&localPlayersList); errRead != nil {
slog.Error("Failed to parse local player list", errAttr(errRead))
} else {
count, errPlayerImport := rulesEngine.ImportPlayers(&localPlayersList)
if errPlayerImport != nil {
slog.Error("Failed to import local player list", errAttr(errPlayerImport))
} else {
slog.Info("Loaded local player list", slog.Int("count", count))
}
}
LogClose(input)
}
}
// Try and load our existing custom rules
if platform.Exists(settings.LocalRulesListPath()) {
input, errInput := os.Open(settings.LocalRulesListPath())
if errInput != nil {
slog.Error("Failed to open local rules list", errAttr(errInput))
} else {
var localRules rules.RuleSchema
if errRead := json.NewDecoder(input).Decode(&localRules); errRead != nil {
slog.Error("Failed to parse local rules list", errAttr(errRead))
} else {
count, errRulesImport := rulesEngine.ImportRules(&localRules)
if errRulesImport != nil {
slog.Error("Failed to import local rules list", errAttr(errRulesImport))
}
slog.Debug("Loaded local rules list", slog.Int("count", count))
}
LogClose(input)
}
}
}
return rulesEngine
}
// openApplicationPage launches the http frontend using the platform specific browser launcher function.
func openApplicationPage(plat platform.Platform, appURL string) {
if errOpen := plat.OpenURL(appURL); errOpen != nil {
slog.Error("Failed to open URL", slog.String("url", appURL), errAttr(errOpen))
}
}
func isErrorAddressAlreadyInUse(err error) bool {
var errOpError *net.OpError
ok := errors.As(err, &errOpError)
if !ok {
return false
}
var errSyscallError *os.SyscallError
ok = errors.As(errOpError.Err, &errSyscallError)
if !ok {
return false
}
var errErrno syscall.Errno
ok = errors.As(errSyscallError.Err, &errErrno)
if !ok {
return false
}
if errors.Is(errErrno, syscall.EADDRINUSE) {
return true
}
const WSAEADDRINUSE = 10048
if runtime.GOOS == "windows" && errErrno == WSAEADDRINUSE {
return true
}
return false
}
var errConfigRootCreate = errors.New("failed to create config root")
func ensureConfigPath() (string, error) {
configPath := configdir.LocalConfig("bd")
if errMakePath := os.MkdirAll(configPath, 0o700); errMakePath != nil {
return "", errors.Join(errMakePath, errConfigRootCreate)
}
return configPath, nil
}
func run() int {
versionInfo := Version{Version: version, Commit: commit, Date: date}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
plat := platform.New()
slog.Info("Starting", slog.String("ver", versionInfo.Version),
slog.String("date", versionInfo.Date), slog.String("commit", versionInfo.Commit))
configRoot, errConfigPath := ensureConfigPath()
if errConfigPath != nil {
slog.Error("Failed to setup config root", errAttr(errConfigPath))
return 1
}
db, dbCloser, errDB := store.CreateDB(path.Join(configRoot, "bd.sqlite?cache=shared"))
if errDB != nil {
slog.Error("failed to create database", errAttr(errDB))
return 1
}
defer dbCloser()
settingsMgr := newSettingsManager(configRoot, db, plat)
settings, errSettings := settingsMgr.settings(ctx)
if errSettings != nil {
slog.Error("Failed to read settings from database", errAttr(errSettings))
return 1
}
logCloser := MustCreateLogger(settings)
defer logCloser()
rcon := newRconConnection(settings.Rcon.String(), settings.Rcon.Password)
state := newGameState(db, settingsMgr, newPlayerStates(), rcon, db)
parser := newLogParser()
broadcaster := newEventBroadcaster()
var logSrc backgroundService
ingest, errLogReader := newLogIngest(filepath.Join(settings.Tf2Dir, "console.log"), parser, true, broadcaster)
if errLogReader != nil {
slog.Error("Failed to create log startEventEmitter", errAttr(errLogReader))
return 1
}
go testLogFeeder(ctx, ingest)
logSrc = ingest
chat := newChatRecorder(db, broadcaster)
broadcaster.registerConsumer(state.eventChan, EvtAny)
dataSource, errDataSource := newDataSource(settings)
if errDataSource != nil {
slog.Error("failed to create data source", errAttr(errDataSource))
return 1
}
re := createRulesEngine(settings)
cache, cacheErr := NewCache(configRoot, DurationCacheTimeout)
if cacheErr != nil {
slog.Error("Failed to set up cache", errAttr(cacheErr))
return 1
}
lm := newListManager(cache, re, settingsMgr)
updater := newPlayerDataLoader(db, dataSource, settingsMgr, re, state.profileUpdateQueue, state.playerDataChan)
discordPresence := newDiscordState(state, settingsMgr)
processHandler := newProcessState(plat, rcon, settingsMgr)
statusHandler := newStatusUpdater(rcon, processHandler, state, time.Second*2)
bigBrotherHandler := newOverwatch(settingsMgr, rcon, state)
mux, errRoutes := createHandlers(ctx, db, state, processHandler, settingsMgr, re, rcon)
if errRoutes != nil {
slog.Error("failed to create http handlers", errAttr(errRoutes))
return 1
}
httpServer := newHTTPServer(ctx, settings.HttpListenAddr, mux)
// Start all the background workers
for _, svc := range []backgroundService{discordPresence, chat, logSrc, updater, statusHandler, &bigBrotherHandler, processHandler, state} {
go svc.start(ctx)
}
go func() {
if err := lm.start(ctx); err != nil {
slog.Error("Failed to start list manager", errAttr(err))
}
}()
go func() {
time.Sleep(time.Second * 3)
if settings.AutoLaunchGame {
go processHandler.launchGame(settings)
}
if settings.RunMode == ModeRelease {
openApplicationPage(plat, settings.AppURL())
}
}()
go func() {
if errServe := httpServer.ListenAndServe(); errServe != nil && !errors.Is(errServe, http.ErrServerClosed) {
if isErrorAddressAlreadyInUse(errServe) {
// Exit early on bind error.
slog.Error("Listen address already in use", errAttr(errServe))
stop()
return
}
slog.Error("Unhandled error trying to shutdown http service", errAttr(errServe))
}
}()
if settings.SystrayEnabled {
slog.Debug("Using systray")
tray := newAppSystray(plat, settingsMgr, processHandler)
systray.Run(tray.OnReady(ctx), func() {
stop()
})
}
<-ctx.Done()
timeout, cancelHTTP := context.WithTimeout(context.Background(), time.Second*15)
defer cancelHTTP()
if errShutdown := httpServer.Shutdown(timeout); errShutdown != nil {
slog.Error("Failed to shutdown cleanly", errAttr(errShutdown))
} else {
slog.Debug("HTTP Service shutdown successfully")
}
return 0
}
func main() {
os.Exit(run())
}