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)
|
||||
|
||||
Vendored
+26
-1
@@ -1,6 +1,26 @@
|
||||
package env
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func init() {
|
||||
AppName = GetEnvDefault("APP_NAME", "default")
|
||||
Namespace = GetEnvDefault("NAMESPACE", "default")
|
||||
AppType = SAppType(GetEnvDefault("APP_TYPE", string(AppTypeAPI)))
|
||||
}
|
||||
|
||||
var AppName string
|
||||
var Namespace string
|
||||
var AppType SAppType
|
||||
|
||||
type SAppType string
|
||||
|
||||
const (
|
||||
AppTypeAPI = SAppType("api") // api应用类型
|
||||
AppTypeService = SAppType("service") // service应用类型
|
||||
)
|
||||
|
||||
func GetEnvDefault(key, defVal string) string {
|
||||
val, ex := os.LookupEnv(key)
|
||||
@@ -10,3 +30,8 @@ func GetEnvDefault(key, defVal string) string {
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// GetAppNameWithType 获取带应用类型的应用名
|
||||
func GetAppNameWithType() string {
|
||||
return fmt.Sprintf("%s-%s", AppType, AppName)
|
||||
}
|
||||
|
||||
+5
-5
@@ -11,8 +11,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
EndPoints = "endpoints"
|
||||
EndAddress = "etcd:2379"
|
||||
EndPoints = "ENDPOINTS"
|
||||
EndAddress = "etcd1:2379"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -40,11 +40,11 @@ func GetValue(key string) (string, error) {
|
||||
}
|
||||
kv := clientV3.NewKV(client)
|
||||
defer client.Close()
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*3)
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute*3)
|
||||
defer cancel()
|
||||
if getResp, err := kv.Get(ctx, key, clientV3.WithPrefix()); err == nil {
|
||||
for _, v := range getResp.Kvs {
|
||||
return string(v.Value), nil
|
||||
if len(getResp.Kvs) > 0 {
|
||||
return string(getResp.Kvs[0].Value), nil
|
||||
}
|
||||
} else {
|
||||
return "", errors.New("etcd 连接超时")
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/pkg/config"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func init() {
|
||||
dsn, err := config.GetMysql()
|
||||
if err != nil {
|
||||
fmt.Println("init db error: ", err)
|
||||
return
|
||||
}
|
||||
DB, err = gorm.Open(mysql.Open(dsn))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user