grain/config/loadConfig.go

65 lines
1.3 KiB
Go
Raw Normal View History

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
"gopkg.in/yaml.v2"
)
2024-08-03 18:27:58 +00:00
var (
cfg *configTypes.ServerConfig
whitelistCfg *configTypes.WhitelistConfig
once sync.Once
whitelistOnce sync.Once
2024-08-03 18:27: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) {
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
}
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 {
return cfg
2024-08-03 18:27:58 +00:00
}
func GetWhitelistConfig() *configTypes.WhitelistConfig {
return whitelistCfg
}