Skip to content

Commit

Permalink
fix(template): fix gateway template dynamic conf feature
Browse files Browse the repository at this point in the history
fix(template): fix gateway template dynamic conf feature

fix(template): fix gateway template dynamic conf feature

fix(template): fix gateway template dynamic conf feature

fix(template): fix gateway template dynamic conf feature

fix(template): fix gateway template dynamic conf feature
  • Loading branch information
jaronnie committed Dec 19, 2024
1 parent 7148d1c commit fb9fb36
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
15 changes: 7 additions & 8 deletions .template/frame/gateway/app/cmd/server.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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()
}
Expand Down
2 changes: 0 additions & 2 deletions .template/frame/gateway/app/internal/config/config.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/zeromicro/go-zero/zrpc"
)

var C Config

type Config struct {
Zrpc ZrpcConf
Gateway GatewayConf
Expand Down
25 changes: 16 additions & 9 deletions .template/frame/gateway/app/internal/custom/custom.go.tpl
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
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.
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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -25,9 +24,6 @@ func (sc *ServiceContext) DynamicConfListener(cc configurator.Configurator[confi
logx.SetLevel(logx.SevereLevel)
}
}

config.C = v
sc.Config = v
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit fb9fb36

Please sign in to comment.