package config import ( "os" "gopkg.in/yaml.v2" ) type DB struct { Address string `yaml:"addr"` Auth string `yaml:"auth"` Name string `yaml:"name"` ShowLog bool `yaml:"showLog"` MaxLifeTime int `yaml:"maxLifeTime"` MaxIdleTime int `yaml:"maxIdleTime"` MaxOpenConnections int `yaml:"maxOpenConnections"` MaxIdleConnections int `yaml:"maxIdleConnections"` } type Image struct { Dir string `yaml:"dir"` } type Geo struct { Key string `yaml:"key"` Secret string `yaml:"secret"` URL string `yaml:"url"` } type Config struct { Database DB `yaml:"database"` Image Image `yaml:"image"` Geo Geo `yaml:"geo"` Listen string `yaml:"listen"` } func (c *Config) Save(f string) error { data, err := yaml.Marshal(c) if err != nil { return err } err = os.WriteFile(f, data, os.ModePerm) return err } func NewConfig(file string) (*Config, error) { data, err := os.ReadFile(file) if err != nil { return nil, err } conf := &Config{} err = yaml.Unmarshal(data, conf) return conf, err } func NewStrConfig(confStr string) (*Config, error) { cfg := &Config{} err := yaml.Unmarshal([]byte(confStr), cfg) return cfg, err }