/
file.go
199 lines (170 loc) · 4.99 KB
/
file.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
package config
import (
"context"
"encoding/json"
"github.com/GSH-LAN/Unwindia_common/src/go/matchservice"
template2 "github.com/GSH-LAN/Unwindia_common/src/go/template"
"github.com/GSH-LAN/Unwindia_common/src/go/unwindiaError"
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog/log"
"os"
"path"
"reflect"
"strconv"
"strings"
"sync"
"text/template"
)
type ConfigFileImpl struct {
ctx context.Context
currentConfig *Config
configFilename string
templatesDirectory string
watcher *fsnotify.Watcher
lock sync.RWMutex
}
func (c *ConfigFileImpl) GetGameServerTemplateForMatch(info matchservice.MatchInfo) (*GamerServerConfigTemplate, error) {
gameName := info.Game
gameValid := false
var gsTemplate GamerServerConfigTemplate
for game, cfg := range c.GetConfig().UnwindiaPteroConfig.Configs {
if game == gameName {
gameValid = true
gsTemplate = cfg
break
}
}
if !gameValid {
return nil, unwindiaError.NewInvalidGameError(gameName)
}
// TODO: make this shit reliable even with kinda broken configs
// we now parse environments to replace custom variables and convert numeric values
var newEnvironment = make(map[string]interface{})
for envName, envValue := range gsTemplate.Environment {
if val, ok := envValue.(string); ok {
parsedEnvVar := strings.Builder{}
err := template.Must(template.New("serverEnvironment").Funcs(template2.TemplateFunctions).Parse(val)).Execute(&parsedEnvVar, nil)
if err != nil {
log.Error().Err(err).Msg("Error parsing environment template")
} else {
val = parsedEnvVar.String()
}
if intValue, err := strconv.Atoi(val); err == nil {
envValue = intValue
log.Trace().Str("environment", envName).Int("value", intValue).Msg("Environment is type int (parsed from string)")
} else {
envValue = val
log.Trace().Str("environment", envName).Str("value", val).Msg("Environment is type string")
}
}
newEnvironment[envName] = envValue
}
gsTemplate.Environment = newEnvironment
return &gsTemplate, nil
}
func (c *ConfigFileImpl) GetGameServerTemplate(gameName string) (*GamerServerConfigTemplate, error) {
return c.GetGameServerTemplateForMatch(matchservice.MatchInfo{Game: gameName})
}
func NewConfigFile(ctx context.Context, filename, templatesDirectory string) (ConfigClient, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// fileWatcher for config-file
err = watcher.Add(filename)
if err != nil {
log.Fatal().Err(err).Msg("Error adding config file watcher")
}
// fileWatcher for template-files
err = watcher.Add(templatesDirectory)
if err != nil {
log.Fatal().Err(err).Msg("Error adding template file watcher")
}
go func() {
for {
// wait for context closed
select {
case <-ctx.Done():
_ = watcher.Close()
}
}
}()
cfg := ConfigFileImpl{
ctx: ctx,
configFilename: filename,
templatesDirectory: templatesDirectory,
watcher: watcher,
}
_, err = cfg.loadConfig()
if err != nil {
return nil, err
}
go cfg.startFileWatcher()
return &cfg, nil
}
func (c *ConfigFileImpl) GetConfig() *Config {
c.lock.RLock()
defer c.lock.RUnlock()
if c.currentConfig == nil {
_, err := c.loadConfig()
if err != nil {
log.Error().Err(err).Msg("Error loading config")
}
}
return c.currentConfig
}
func (c *ConfigFileImpl) loadConfig() (*Config, error) {
c.lock.Lock()
defer c.lock.Unlock()
var cfg Config
configFile, err := os.ReadFile(c.configFilename)
if err != nil {
log.Error().Err(err).Str("filename", c.configFilename).Msg("Error loading config file")
return nil, err
}
if err = json.Unmarshal(configFile, &cfg); err != nil {
return c.currentConfig, err
}
log.Info().Msgf("Loaded config from file: %+v", cfg)
templates := make(map[string]string)
if files, err := os.ReadDir(c.templatesDirectory); err != nil {
log.Warn().Err(err).Msg("Error loading templates")
} else {
for _, file := range files {
if filecontent, err := os.ReadFile(path.Join(c.templatesDirectory, file.Name())); err != nil {
log.Warn().Err(err).Str("filename", file.Name()).Msg("Error loading content of file")
} else {
templates[file.Name()] = string(filecontent)
}
}
log.Info().Msgf("Loaded templates: %+v", templates)
cfg.Templates = templates
}
if !reflect.DeepEqual(cfg, Config{}) || c.currentConfig == nil {
c.currentConfig = &cfg
}
return c.currentConfig, nil
}
func (c *ConfigFileImpl) startFileWatcher() {
for {
select {
case event, ok := <-c.watcher.Events:
if !ok {
return
}
log.Debug().Msgf("Config file watcher event: %+v", event)
if event.Has(fsnotify.Create) || event.Has(fsnotify.Write) || event.Has(fsnotify.Chmod) {
log.Info().Str("event", event.Name).Msgf("modified config file")
_, err := c.loadConfig()
if err != nil {
log.Error().Err(err).Msg("Error loading config from file")
}
}
case err, ok := <-c.watcher.Errors:
if !ok {
return
}
log.Error().Err(err).Msg("Filewatcher encountered error")
}
}
}