backend/pkg/config/config.go

52 lines
917 B
Go
Raw Normal View History

2022-01-11 00:48:03 +08:00
package config
import (
"encoding/json"
"fmt"
"git.icechen.cn/monorepo/backend/pkg/etcd"
"github.com/pkg/errors"
)
const (
prefix = "/config"
env = "/env"
)
const (
EnvTest = "test"
EnvOnl = "onl"
)
// GetConfig 根据etcd获取存入配置
func GetConfig(name string, config interface{}) error {
key := fmt.Sprintf("%v/config/%v", prefix, name)
value, err := etcd.GetValue(key)
if err != nil {
return err
}
err = json.Unmarshal([]byte(value), config)
if err != nil {
return errors.WithMessage(err, "etcd配置解析到json失败")
}
return nil
}
func GetMysql(name string) (string, error) {
key := fmt.Sprintf("%v/mysql/%v", prefix, name)
return etcd.GetValue(key)
}
func GetEnv() (string, error) {
return etcd.GetValue(env)
}
func IsTest() bool {
envString, _ := GetEnv()
return envString == EnvTest
}
func IsOnl() bool {
envString, _ := GetEnv()
return envString == EnvOnl
}