diff --git a/.template/frame/gateway/app/cmd/server.go.tpl b/.template/frame/gateway/app/cmd/server.go.tpl index 4498ae64..59a82af2 100644 --- a/.template/frame/gateway/app/cmd/server.go.tpl +++ b/.template/frame/gateway/app/cmd/server.go.tpl @@ -4,10 +4,11 @@ import ( "os" "path/filepath" + configurator "github.com/zeromicro/go-zero/core/configcenter" + "github.com/jzero-io/jzero-contrib/dynamic_conf" "github.com/common-nighthawk/go-figure" "github.com/jzero-io/jzero-contrib/embedx" "github.com/spf13/cobra" - "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/service" "github.com/zeromicro/go-zero/gateway" @@ -25,20 +26,16 @@ var serverCmd = &cobra.Command{ Short: "{{ .APP }} server", Long: "{{ .APP }} server", Run: func(cmd *cobra.Command, args []string) { - var c config.Config - conf.MustLoad(cfgFile, &c) - config.C = c + ss, err := dynamic_conf.NewFsNotify(cfgFile) + logx.Must(err) + cc := configurator.MustNewConfigCenter[config.Config](configurator.Config{ + Type: "yaml", + }, ss) + c, err := cc.GetConfig() + logx.Must(err) + config.C = c - // write pb to local - var err error - c.Gateway.Upstreams[0].ProtoSets, err = embedx.WriteToLocalTemp(pb.Embed, embedx.WithFileMatchFunc(func(path string) bool { - return filepath.Ext(path) == ".pb" - })) - if err != nil { - logx.Must(err) - } - - // set up logger + // set up logger if err = logx.SetUp(c.Log.LogConf); err != nil { logx.Must(err) } @@ -46,8 +43,14 @@ var serverCmd = &cobra.Command{ logx.AddWriter(logx.NewWriter(os.Stdout)) } - ctx := svc.NewServiceContext(c) - run(ctx) + // write pb to local + c.Gateway.Upstreams[0].ProtoSets, err = embedx.WriteToLocalTemp(pb.Embed, embedx.WithFileMatchFunc(func(path string) bool { + return filepath.Ext(path) == ".pb" + })) + logx.Must(err) + + svcCtx := svc.NewServiceContext(c, cc) + run(svcCtx) }, } diff --git a/.template/frame/gateway/app/internal/svc/dynamic_conf.go.tpl b/.template/frame/gateway/app/internal/svc/dynamic_conf.go.tpl new file mode 100644 index 00000000..9bfa9d1d --- /dev/null +++ b/.template/frame/gateway/app/internal/svc/dynamic_conf.go.tpl @@ -0,0 +1,33 @@ +package svc + +import ( + configurator "github.com/zeromicro/go-zero/core/configcenter" + "github.com/zeromicro/go-zero/core/logx" + + "{{ .Module }}/internal/config" +) + +func (sc *ServiceContext) DynamicConfListener(cc configurator.Configurator[config.Config]) { + cc.AddListener(func() { + logLevel := sc.Config.Log.Level + logx.Infof("config file changed") + if v, err := cc.GetConfig(); err == nil { + if v.Log.Level != logLevel { + logx.Infof("log level changed: %s", v.Log.Level) + switch v.Log.Level { + case "debug": + logx.SetLevel(logx.DebugLevel) + case "info": + logx.SetLevel(logx.InfoLevel) + case "error": + logx.SetLevel(logx.ErrorLevel) + case "severe": + logx.SetLevel(logx.SevereLevel) + } + } + + config.C = v + sc.Config = v + } + }) +} diff --git a/.template/frame/gateway/app/internal/svc/service_context.go.tpl b/.template/frame/gateway/app/internal/svc/service_context.go.tpl index ec8a8a1e..97fcf650 100644 --- a/.template/frame/gateway/app/internal/svc/service_context.go.tpl +++ b/.template/frame/gateway/app/internal/svc/service_context.go.tpl @@ -3,6 +3,8 @@ package svc import ( "{{ .Module }}/internal/config" "{{ .Module }}/internal/custom" + + configurator "github.com/zeromicro/go-zero/core/configcenter" ) type ServiceContext struct { @@ -11,9 +13,11 @@ type ServiceContext struct { Custom *custom.Custom } -func NewServiceContext(c config.Config) *ServiceContext { - return &ServiceContext{ +func NewServiceContext(c config.Config, cc configurator.Configurator[config.Config]) *ServiceContext { + sc := &ServiceContext{ Config: c, Custom: custom.New(), } + sc.DynamicConfListener(cc) + return sc }