generated from pkg/go-template
+32
-10
@@ -3,48 +3,70 @@ package config
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/pkg/env"
|
||||
"git.icechen.cn/monorepo/backend/pkg/etcd"
|
||||
"github.com/pkg/errors"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
const (
|
||||
prefix = "/config"
|
||||
env = "/env"
|
||||
envKey = "/env"
|
||||
)
|
||||
|
||||
type Env string
|
||||
|
||||
const (
|
||||
EnvTest = "test"
|
||||
EnvOnl = "onl"
|
||||
EnvTest Env = "test"
|
||||
EnvOnl Env = "onl"
|
||||
)
|
||||
|
||||
// GetConfig 根据etcd获取存入配置
|
||||
func GetConfig(name string, config interface{}) error {
|
||||
key := fmt.Sprintf("%v/config/%v", prefix, name)
|
||||
func GetConfig(config interface{}) error {
|
||||
return Bind("config", config)
|
||||
}
|
||||
|
||||
// GetMysql 获取数据库连接字符串
|
||||
func GetMysql() (string, error) {
|
||||
return GetString("mysql")
|
||||
}
|
||||
|
||||
// Bind 读取配置项到结构体
|
||||
func Bind(item string, out interface{}) error {
|
||||
if reflect.TypeOf(out).Kind() != reflect.Ptr {
|
||||
return errors.New("bind对象不是指针类型")
|
||||
}
|
||||
key := fmt.Sprintf("%s/%s/%s/%s", prefix, env.Namespace, env.GetAppNameWithType(), item)
|
||||
value, err := etcd.GetValue(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal([]byte(value), config)
|
||||
err = json.Unmarshal([]byte(value), out)
|
||||
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)
|
||||
// GetString 读取配置项字符串
|
||||
func GetString(item string) (string, error) {
|
||||
key := fmt.Sprintf("%s/%s/%s/%s", prefix, env.Namespace, env.GetAppNameWithType(), item)
|
||||
return etcd.GetValue(key)
|
||||
}
|
||||
|
||||
func GetEnv() (string, error) {
|
||||
return etcd.GetValue(env)
|
||||
// GetEnv 获取环境
|
||||
func GetEnv() (Env, error) {
|
||||
envString, err := etcd.GetValue(envKey)
|
||||
return Env(envString), err
|
||||
}
|
||||
|
||||
// IsTest 是否是测试环境
|
||||
func IsTest() bool {
|
||||
envString, _ := GetEnv()
|
||||
return envString == EnvTest
|
||||
}
|
||||
|
||||
// IsOnl 是否是正式环境
|
||||
func IsOnl() bool {
|
||||
envString, _ := GetEnv()
|
||||
return envString == EnvOnl
|
||||
|
||||
@@ -2,6 +2,8 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -10,24 +12,11 @@ type config struct {
|
||||
Es string `json:"es,omitempty"`
|
||||
}
|
||||
|
||||
func TestGetMysql(t *testing.T) {
|
||||
//获取 /config/mysql/api-zeus 的str
|
||||
mysql, err := GetMysql("api-zeus")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(mysql)
|
||||
}
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
c := new(config)
|
||||
err := GetConfig("api-zeus", c)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnv(t *testing.T) {
|
||||
resp, err := http.Get("http://api-lark-test/user")
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
t.Log(string(b))
|
||||
|
||||
env, err := GetEnv()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
Reference in New Issue
Block a user