diff --git a/.template/frame/gateway/app/cmd/server.go.tpl b/.template/frame/gateway/app/cmd/server.go.tpl index 59a82af2..872af460 100644 --- a/.template/frame/gateway/app/cmd/server.go.tpl +++ b/.template/frame/gateway/app/cmd/server.go.tpl @@ -33,7 +33,6 @@ var serverCmd = &cobra.Command{ }, ss) c, err := cc.GetConfig() logx.Must(err) - config.C = c // set up logger if err = logx.SetUp(c.Log.LogConf); err != nil { @@ -50,13 +49,13 @@ var serverCmd = &cobra.Command{ logx.Must(err) svcCtx := svc.NewServiceContext(c, cc) - run(svcCtx) + run(c, svcCtx) }, } -func run(svcCtx *svc.ServiceContext) { - zrpc := server.RegisterZrpc(svcCtx.Config, svcCtx) - gw := gateway.MustNewServer(svcCtx.Config.Gateway.GatewayConf, middleware.WithHeaderProcessor()) +func run(c config.Config, svcCtx *svc.ServiceContext) { + zrpc := server.RegisterZrpc(c, svcCtx) + gw := gateway.MustNewServer(c.Gateway.GatewayConf, middleware.WithHeaderProcessor()) // register middleware middleware.Register(zrpc, gw) @@ -69,9 +68,9 @@ func run(svcCtx *svc.ServiceContext) { group.Add(gw) group.Add(svcCtx.Custom) - printBanner(svcCtx.Config) - logx.Infof("Starting rpc server at %s...", svcCtx.Config.Zrpc.ListenOn) - logx.Infof("Starting gateway server at %s:%d...", svcCtx.Config.Gateway.Host, svcCtx.Config.Gateway.Port) + printBanner(c) + logx.Infof("Starting rpc server at %s...", c.Zrpc.ListenOn) + logx.Infof("Starting gateway server at %s:%d...", c.Gateway.Host, c.Gateway.Port) group.Start() } diff --git a/.template/frame/gateway/app/internal/config/config.go.tpl b/.template/frame/gateway/app/internal/config/config.go.tpl index c018c393..97e5d656 100644 --- a/.template/frame/gateway/app/internal/config/config.go.tpl +++ b/.template/frame/gateway/app/internal/config/config.go.tpl @@ -6,8 +6,6 @@ import ( "github.com/zeromicro/go-zero/zrpc" ) -var C Config - type Config struct { Zrpc ZrpcConf Gateway GatewayConf diff --git a/.template/frame/gateway/app/internal/custom/custom.go.tpl b/.template/frame/gateway/app/internal/custom/custom.go.tpl index daf6673b..d3d892c0 100644 --- a/.template/frame/gateway/app/internal/custom/custom.go.tpl +++ b/.template/frame/gateway/app/internal/custom/custom.go.tpl @@ -1,15 +1,19 @@ package custom import ( - "os" + "os" - "{{.Module}}/internal/config" + configurator "github.com/zeromicro/go-zero/core/configcenter" + + "{{.Module}}/internal/config" ) -type Custom struct{} +type Custom struct { + Config configurator.Configurator[config.Config] +} -func New() *Custom { - return &Custom{} +func New(config configurator.Configurator[config.Config]) *Custom { + return &Custom{Config: config} } // Start Please add custom logic here. @@ -17,10 +21,13 @@ func (c *Custom) Start() {} // Stop Please add shut down logic here. func (c *Custom) Stop() { - // remove temp pb file - if len(config.C.Gateway.Upstreams) > 0 { - for _, p := range config.C.Gateway.Upstreams[0].ProtoSets { - _ = os.Remove(p) + conf, err := c.Config.GetConfig() + if err == nil { + // remove temp pb file + if len(conf.Gateway.Upstreams) > 0 { + for _, p := range conf.Gateway.Upstreams[0].ProtoSets { + _ = os.Remove(p) + } } } } diff --git a/.template/frame/gateway/app/internal/svc/dynamic_conf.go.tpl b/.template/frame/gateway/app/internal/svc/config.go.tpl similarity index 75% rename from .template/frame/gateway/app/internal/svc/dynamic_conf.go.tpl rename to .template/frame/gateway/app/internal/svc/config.go.tpl index 9bfa9d1d..1fc67b27 100644 --- a/.template/frame/gateway/app/internal/svc/dynamic_conf.go.tpl +++ b/.template/frame/gateway/app/internal/svc/config.go.tpl @@ -7,12 +7,11 @@ import ( "{{ .Module }}/internal/config" ) -func (sc *ServiceContext) DynamicConfListener(cc configurator.Configurator[config.Config]) { +func (sc *ServiceContext) SetConfigListener(c config.Config, 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 { + if v.Log.Level != c.Log.Level { logx.Infof("log level changed: %s", v.Log.Level) switch v.Log.Level { case "debug": @@ -25,9 +24,6 @@ func (sc *ServiceContext) DynamicConfListener(cc configurator.Configurator[confi 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 97fcf650..e816a0fc 100644 --- a/.template/frame/gateway/app/internal/svc/service_context.go.tpl +++ b/.template/frame/gateway/app/internal/svc/service_context.go.tpl @@ -8,16 +8,16 @@ import ( ) type ServiceContext struct { - Config config.Config + Config configurator.Configurator[config.Config] Custom *custom.Custom } func NewServiceContext(c config.Config, cc configurator.Configurator[config.Config]) *ServiceContext { sc := &ServiceContext{ - Config: c, - Custom: custom.New(), + Config: cc, + Custom: custom.New(cc), } - sc.DynamicConfListener(cc) + sc.SetConfigListener(c, cc) return sc }