36 lines
611 B
Go
36 lines
611 B
Go
|
package redis
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
|
||
|
"gopkg.in/yaml.v3"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
DefaultConfigPath = "/cfg/redis.yaml"
|
||
|
ConfigPathEnvKey = "REDIS_CONFIG_PATH"
|
||
|
)
|
||
|
|
||
|
type Option struct {
|
||
|
Addr string `yaml:"addr"`
|
||
|
Password string `yaml:"password"`
|
||
|
DB int `yaml:"db"`
|
||
|
}
|
||
|
|
||
|
func LoadDefaultConfig() (*Option, error) {
|
||
|
path := os.Getenv(ConfigPathEnvKey)
|
||
|
if path == "" {
|
||
|
path = DefaultConfigPath
|
||
|
}
|
||
|
return LoadConfig(path)
|
||
|
}
|
||
|
func LoadConfig(path string) (*Option, error) {
|
||
|
var opt Option
|
||
|
f, err := os.Open(path)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
return &opt, yaml.NewDecoder(f).Decode(&opt)
|
||
|
}
|