grain/config/loadConfig.go

39 lines
531 B
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
once sync.Once
)
func LoadConfig(filename string) (*configTypes.ServerConfig, error) {
2024-07-31 15:56:55 +00:00
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
2024-08-03 18:27:58 +00:00
var config configTypes.ServerConfig
2024-07-31 15:56:55 +00:00
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
2024-08-03 18:27:58 +00:00
once.Do(func() {
cfg = &config
})
return cfg, nil
}
func GetConfig() *configTypes.ServerConfig {
return cfg
}