Skip to content

Commit

Permalink
feat(templates): gateway templates default add dynamic conf features
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Dec 11, 2024
1 parent 7afac7b commit 9847ac1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
35 changes: 19 additions & 16 deletions .template/frame/gateway/app/cmd/server.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,29 +26,31 @@ 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)
}
if c.Log.LogConf.Mode != "console" {
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)
},
}

Expand Down
33 changes: 33 additions & 0 deletions .template/frame/gateway/app/internal/svc/dynamic_conf.go.tpl
Original file line number Diff line number Diff line change
@@ -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
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package svc
import (
"{{ .Module }}/internal/config"
"{{ .Module }}/internal/custom"

configurator "github.com/zeromicro/go-zero/core/configcenter"
)

type ServiceContext struct {
Expand All @@ -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
}

0 comments on commit 9847ac1

Please sign in to comment.