提交 61d7db49 authored 作者: mooncake9527's avatar mooncake9527

update

上级 9f4ce0e8
......@@ -94,7 +94,7 @@ type Config struct {
ConfType string `yaml:"confType" json:"confType"`
Local Local `yaml:"-" json:"local"`
Consul Consul `yaml:"-" json:"consul"`
Nacos onacos.NacosConfig `yaml:"-" json:"nacos"`
Nacos onacos.NacosConfig `yaml:"nacos" json:"nacos"`
Database []Database `yaml:"database" json:"database"`
Etcd Etcd `yaml:"etcd" json:"etcd"`
......
......@@ -29,10 +29,9 @@ func parse() (err error) {
if hostname := os.Getenv("HOSTNAME"); hostname != "" {
Cfg.App.PodName = hostname
}
confType := loadEnvWithDefault("CONF_TYPE", "local")
confType := readEnvVarWithDefault("CONF_TYPE", "local")
logger.Infof("[conf]conf type:%s", confType)
Cfg.ConfType = confType
if confType == "local" {
localConf()
logger.Infof("[conf]conf:%s", Cfg.Local.Conf)
......@@ -60,6 +59,7 @@ func parse() (err error) {
if err := v.Unmarshal(&Cfg); err != nil {
return err
}
logger.Debug("[conf]parse conf", logger.Any("conf", Cfg))
return ParseExtend()
}, logger.Get())
if err := nc.Connect().Watch().Fetch().Parse().Err(); err != nil {
......@@ -78,7 +78,6 @@ func ParseExtend() error {
v := viper.New()
v.SetConfigType("yaml")
content := string(Cfg.GetContent())
// if err := v.ReadConfig(bytes.NewReader(Cfg.GetContent())); err != nil {
if err := v.ReadConfig(strings.NewReader(content)); err != nil {
return err
}
......@@ -89,26 +88,26 @@ func ParseExtend() error {
if err := subv.Unmarshal(extend); err != nil {
return err
}
logger.Info("[conf]", logger.Any("extend", extend))
logger.Debug("[conf]parse extend", logger.Any("extend", extend))
}
}
return nil
}
func localConf() {
confFile := loadEnvWithDefault("CONF_FILES", "./conf/conf.yaml")
confFile := readEnvVarWithDefault("CONF_FILES", "./conf/conf.yaml")
Write(func(c *Config) {
c.Local.Conf = confFile
})
}
func nacosConf() {
host := loadEnvWithDefault("NACOS_HOST", "localhost")
port := loadEnvWithDefault("NACOS_PORT", "8848")
contextPath := loadEnvWithDefault("NACOS_CONTEXT", "/nacos")
namespace := loadEnvWithDefault("NACOS_NAMESPACE", "")
dataID := loadEnvWithDefault("NACOS_DATA_ID", "operator-admin.yaml")
group := loadEnvWithDefault("NACOS_GROUP", "qitu")
host := readEnvVarWithDefault("NACOS_HOST", "localhost")
port := readEnvVarWithDefault("NACOS_PORT", "8848")
contextPath := readEnvVarWithDefault("NACOS_CONTEXT", "/nacos")
namespace := readEnvVarWithDefault("NACOS_NAMESPACE", "")
dataID := readEnvVarWithDefault("NACOS_DATA_ID", "conf.yaml")
group := readEnvVarWithDefault("NACOS_GROUP", "DEFAULT_GROUP")
logger.Infof("[conf]conf host:%s namespace:%s dataId:%s group:%s", host, namespace, dataID, group)
Cfg.Nacos.IPs = strings.Split(host, ",")
Cfg.Nacos.Port = cast.ToInt(port)
......@@ -116,6 +115,9 @@ func nacosConf() {
Cfg.Nacos.Conf.DataID = dataID
Cfg.Nacos.Conf.Group = group
Cfg.Nacos.Conf.ContextPath = contextPath
Cfg.Nacos.Rd.GroupName = group
}
func consulConf() {
......@@ -136,7 +138,7 @@ func consulConf() {
}
}
func loadEnvWithDefault(key, defaultValue string) string {
func readEnvVarWithDefault(key, defaultValue string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
......
......@@ -44,6 +44,7 @@ logger:
database:
- name: "wz_qitu"
driver: "mysql"
default: true
mysql:
dsn: "root:123456@tcp(localhost:30006)/wz_qitu?parseTime=true&loc=Local&charset=utf8,utf8mb4"
enableLog: true
......@@ -57,6 +58,15 @@ redis:
readTimeout: 2
writeTimeout: 2
oss:
endpoint: https://oss-cn-hangzhou.aliyuncs.com # 外网
access-key-id: LTAI5tD3VA4pi1pcz48yYgXL
access-key-secret: sus17wfcbjB6J9hDeJXXFtJf9LPN0S
bucket-name: prod-qitu
base-path: baofu
region: oss-cn-hangzhou
bucket-url: https://prod-qitu.oss-cn-hangzhou.aliyuncs.com
auth:
enable: true
expire: 6h
......
......@@ -23,10 +23,16 @@ func InitDB() (err error) {
config.Read(func(c *config.Config) {
databases = c.Database
})
if len(databases) == 1 {
consts.DefaultSchema = databases[0].Name
} else {
for _, db := range databases {
if db.Default {
consts.DefaultSchema = db.Name
}
}
}
for _, db := range databases {
switch strings.ToLower(db.Driver) {
case sgorm.DBDriverMysql, sgorm.DBDriverTidb:
gdb, err := InitMysql(&db)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论