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

update

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