Skip to content

Commit

Permalink
增加远程配置
Browse files Browse the repository at this point in the history
  • Loading branch information
baowenke committed Nov 29, 2023
1 parent 561bd3d commit fee2468
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ dilu.exe
temp/
!temp
vendor
resources/config.dev.yml
*/.DS_Store
**/**.log

# go sum
go.sum
resources/config.test.yaml
resources/config.local.yaml
92 changes: 80 additions & 12 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"dilu/common/config"
"dilu/common/middleware"
"fmt"
"time"

coreCfg "github.com/baowk/dilu-core/config"

"github.com/baowk/dilu-core/core"
"github.com/baowk/dilu-core/core/i18n"

"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
)

var (
Expand All @@ -37,27 +41,76 @@ func run() {
}
v := viper.New()
v.SetConfigFile(configYml)
v.SetConfigType("yaml")
//v.SetConfigType("yaml")
err := v.ReadInConfig()
if err != nil {
panic(fmt.Sprintf("Fatal error config file: %v \n", err))
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.String())
if err = v.Unmarshal(&core.Cfg); err != nil {
fmt.Println(err)

var cfg coreCfg.AppCfg

if err = v.Unmarshal(&cfg); err != nil {
fmt.Println(err)
}

if cfg.Server.RemoteEnable {
rviper := viper.New()
if cfg.Remote.SecretKeyring == "" {
err = rviper.AddRemoteProvider(cfg.Remote.Provider, cfg.Remote.Endpoint, cfg.Remote.Path)
} else {
err = rviper.AddSecureRemoteProvider(cfg.Remote.Provider, cfg.Remote.Endpoint, cfg.Remote.Path, cfg.Remote.SecretKeyring)
}
if err != nil {
panic(fmt.Sprintf("Fatal error remote config : %v \n", err))
}
extend := v.Sub("extend")
rviper.SetConfigType(cfg.Remote.GetConfigType())
err = rviper.ReadRemoteConfig()
if err != nil {
panic(fmt.Sprintf("Fatal error remote config : %v \n", err))
}
var remoteCfg coreCfg.AppCfg
rviper.Unmarshal(&remoteCfg)

mergeCfg(&cfg, &remoteCfg)

extend := rviper.Sub("extend")
if extend != nil {
extend.Unmarshal(config.Ext)
}
})
if err = v.Unmarshal(&core.Cfg); err != nil {
fmt.Println(err)
}
go func() {
for {
time.Sleep(time.Second * 5) // delay after each request
err := rviper.WatchRemoteConfig()
if err != nil {
fmt.Println(err)
continue
}
rviper.Unmarshal(&remoteCfg)

v.Sub("extend").Unmarshal(&config.Ext)
mergeCfg(&cfg, &remoteCfg)

extend := rviper.Sub("extend")
if extend != nil {
extend.Unmarshal(config.Ext)
}
}
}()
} else {
mergeCfg(&cfg, nil)
v.Sub("extend").Unmarshal(&config.Ext)
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.String())
if err = v.Unmarshal(cfg); err != nil {
fmt.Println(err)
}
mergeCfg(&cfg, nil)
extend := v.Sub("extend")
if extend != nil {
extend.Unmarshal(config.Ext)
}
})
}

core.Init()

Expand All @@ -75,3 +128,18 @@ func run() {
}
core.Run()
}

func mergeCfg(local, remote *coreCfg.AppCfg) {
if remote != nil {
core.Cfg = *local
core.Cfg = *remote
core.Cfg.Server.Mode = local.Server.Mode
core.Cfg.Server.RemoteEnable = local.Server.RemoteEnable
core.Cfg.Remote = local.Remote
core.Cfg.Server.Name = local.Server.Name
core.Cfg.Server.Port = local.Server.Port
core.Cfg.Server.Host = local.Server.Host
} else {
core.Cfg = *local
}
}
2 changes: 1 addition & 1 deletion common/config/extend.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package config

var Ext Extend
var Ext *Extend

type Extend struct {
Ding DingCfg `mapstructure:"ding" json:"ding" yaml:"ding"`
Expand Down
39 changes: 38 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible
github.com/aws/aws-sdk-go v1.44.319
github.com/baowk/dilu-core v0.1.9
github.com/baowk/dilu-core v0.2.3
github.com/fsnotify/fsnotify v1.6.0
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt/v5 v5.0.0
Expand Down Expand Up @@ -34,13 +34,22 @@ require (
)

require (
cloud.google.com/go v0.110.0 // indirect
cloud.google.com/go/compute v1.19.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/firestore v1.9.0 // indirect
cloud.google.com/go/longrunning v0.4.1 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/armon/go-metrics v0.4.0 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand All @@ -53,12 +62,26 @@ require (
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/s2a-go v0.1.3 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.8.0 // indirect
github.com/gookit/color v1.3.6 // indirect
github.com/hashicorp/consul/api v1.20.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand All @@ -75,16 +98,19 @@ require (
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.1.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mozillazg/go-httpheader v0.2.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/redis/go-redis/v9 v9.0.5 // indirect
github.com/sagikazarmark/crypt v0.10.0 // indirect
github.com/shamsher31/goimgext v1.0.0 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/spf13/afero v1.9.5 // indirect
Expand All @@ -97,14 +123,25 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.etcd.io/etcd/api/v3 v3.5.9 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.9 // indirect
go.etcd.io/etcd/client/v2 v2.305.7 // indirect
go.etcd.io/etcd/client/v3 v3.5.9 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.122.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
83 changes: 83 additions & 0 deletions resources/config.remote.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
server:
name: dilu-app # 服务名称
remote-enable: true #是否开启远程配置
mode: dev # 应用程序运行模式(dev开发环境、test测试环境、prod线上环境)
host: 0.0.0.0 # 服务器IP地址,默认使用0.0.0.0
port: 7888 # 服务端口号
read-timeout: 60 #读超时 单位秒 默认20
write-timeout: 60 #写超时 单位秒 默认20
fs-type: local #文件服务
remote:
provider: etcd #远程提供者 etcd etcd3 consul ...
endpoint: http://172.29.173.47:2379 #协议地址
path: /dilu/config #路径 key
secret-keyring: #加密 不填为非加密
logger: # 日志配置
level: debug # 级别
prefix: # 日志前缀
format: # 输出格式 json
director: temp/log # 日志文件夹
encode-level: LowercaseColorLevelEncoder # 编码级
stacktrace-key: # 栈名
#max-age: 7 # 日志留存时间 默认7天
show-line: true # 显示行
log-in-console: true # 输出控制台
jwt: # JWT配置
sign-key: 56n1KywHIPEkWWIxffGpp8Dfl3z5SjE5WBeDStc1T64klnpoAqHrHN01vnete123 # Token密钥
expires: 10080 # Token过期时长(单位:分钟)
refresh: 14400 # Token 刷新时长(单位:分钟)
cache: # 缓存配置
type: memory
addr: localhost:6379 # Redis服务器地址
#password: redis # Redis密码
db: 5 # Redis数据库索引
dbcfg: # 数据库配置
driver: mysql
dns: root:12345678@tcp(127.0.0.1:3306)/dilu-db?charset=utf8&parseTime=True&loc=Local&timeout=1000ms # 数据库连接字符串
log-mode: info #日志类型 GORM 定义了这些日志级别:silent、error、warn、info
#slow-threshold: 200 #慢日志
#max-idle-conns: 20 #最大空闲连接数 默认10
#max-open-conns: 60 #最大打开数
# prefix: 日志前缀
dbs:
- dental: #子配置会继承父配置
dns: root:12345678@tcp(127.0.0.1:3306)/dental-db?charset=utf8&parseTime=True&loc=Local&timeout=1000ms # 数据库连接字符串
# disable: true
# driver: mysql
# prefix: 日志前缀
# singular: false #全局复数
# max-idle-conns: 10 #最大空闲连接数 默认10
# max-open-conns: 30 #最大打开数
# max-lifetime: 60 #链接重置时间(分)
- notice: #子配置会继承父配置
dns: root:12345678@tcp(127.0.0.1:3306)/notice-db?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
cors:
enable: true
mode: allow-all
#whitelist:
#- allow-origin: example1.com
# allow-methods: POST, GET
# allow-headers: Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id
# expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,
# Content-Type
# allow-credentials: true
gen:
enable: true #开启生成
front-path: ../dilu-admin/src #前端生成路径 默认:../dilu-admin/src
extend: # 扩展项(此处没有提供具体说明)
ding:
agent-id:
app-key:
app-secret:
crop-id:
wechat-mp:
app-id:
app-secret:
wx-token:
encoding-aes-key:
ai:
ali:
sk:



0 comments on commit fee2468

Please sign in to comment.