common/pkg/redis/option.go

36 lines
611 B
Go
Raw Permalink Normal View History

2024-08-29 20:38:48 +08:00
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)
}