2024-07-31 15:56:55 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2024-08-03 18:27:58 +00:00
|
|
|
"sync"
|
2024-07-31 15:56:55 +00:00
|
|
|
|
2024-08-03 18:27:58 +00:00
|
|
|
configTypes "grain/config/types"
|
2024-07-31 15:56:55 +00:00
|
|
|
|
2024-10-18 15:22:01 +00:00
|
|
|
"grain/server/utils"
|
|
|
|
|
2024-07-31 15:56:55 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2024-08-03 18:27:58 +00:00
|
|
|
var (
|
2024-10-16 13:57:58 +00:00
|
|
|
cfg *configTypes.ServerConfig
|
|
|
|
whitelistCfg *configTypes.WhitelistConfig
|
2024-10-16 18:07:37 +00:00
|
|
|
blacklistCfg *configTypes.BlacklistConfig
|
2024-10-16 13:57:58 +00:00
|
|
|
once sync.Once
|
|
|
|
whitelistOnce sync.Once
|
2024-10-16 18:07:37 +00:00
|
|
|
blacklistOnce sync.Once
|
2024-08-03 18:27:58 +00:00
|
|
|
)
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
// LoadConfig loads the server configuration from config.yml
|
2024-08-03 18:27:58 +00:00
|
|
|
func LoadConfig(filename string) (*configTypes.ServerConfig, error) {
|
2024-10-16 13:57:58 +00:00
|
|
|
data, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var config configTypes.ServerConfig
|
|
|
|
err = yaml.Unmarshal(data, &config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-10-18 15:22:01 +00:00
|
|
|
// Adjust event time constraints after loading
|
|
|
|
utils.AdjustEventTimeConstraints(&config)
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
once.Do(func() {
|
|
|
|
cfg = &config
|
|
|
|
})
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadWhitelistConfig loads the whitelist configuration from whitelist.yml
|
|
|
|
func LoadWhitelistConfig(filename string) (*configTypes.WhitelistConfig, error) {
|
|
|
|
data, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var config configTypes.WhitelistConfig
|
|
|
|
err = yaml.Unmarshal(data, &config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
whitelistOnce.Do(func() {
|
|
|
|
whitelistCfg = &config
|
|
|
|
})
|
|
|
|
|
|
|
|
return whitelistCfg, nil
|
2024-08-03 18:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfig() *configTypes.ServerConfig {
|
2024-10-16 13:57:58 +00:00
|
|
|
return cfg
|
2024-08-03 18:27:58 +00:00
|
|
|
}
|
2024-10-16 13:57:58 +00:00
|
|
|
|
|
|
|
func GetWhitelistConfig() *configTypes.WhitelistConfig {
|
|
|
|
return whitelistCfg
|
2024-10-16 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBlacklistConfig loads the blacklist configuration from blacklist.yml
|
|
|
|
func LoadBlacklistConfig(filename string) (*configTypes.BlacklistConfig, error) {
|
|
|
|
data, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var config configTypes.BlacklistConfig
|
|
|
|
err = yaml.Unmarshal(data, &config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blacklistOnce.Do(func() {
|
|
|
|
blacklistCfg = &config
|
|
|
|
})
|
|
|
|
|
|
|
|
return blacklistCfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBlacklistConfig() *configTypes.BlacklistConfig {
|
|
|
|
return blacklistCfg
|
2024-10-16 13:57:58 +00:00
|
|
|
}
|