Skip to content

Commit

Permalink
fix: change the api to start the app by config-api
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenceLiZhixin committed Nov 6, 2021
1 parent 9f811e9 commit ebc0db4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
13 changes: 9 additions & 4 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ var (
func Load(opts ...LoaderConfOption) error {
// conf
conf := NewLoaderConf(opts...)
koan := GetConfigResolver(conf)
if err := koan.UnmarshalWithConf(rootConfig.Prefix(),
rootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
if conf.rc == nil {
koan := GetConfigResolver(conf)
if err := koan.UnmarshalWithConf(rootConfig.Prefix(),
rootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
}
} else {
rootConfig = conf.rc
}

if err := rootConfig.Init(); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions config/config_loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type loaderConf struct {
delim string
// config bytes
bytes []byte
// user provide rootConfig built by config api
rc *RootConfig
}

func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
Expand All @@ -60,6 +62,9 @@ func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
for _, opt := range opts {
opt.apply(conf)
}
if conf.rc != nil {
return conf
}
if len(conf.bytes) <= 0 {
bytes, err := ioutil.ReadFile(conf.path)
if err != nil {
Expand Down Expand Up @@ -105,6 +110,12 @@ func WithPath(path string) LoaderConfOption {
})
}

func WithRootConfig(rc *RootConfig) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.rc = rc
})
}

func WithDelim(delim string) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.delim = delim
Expand Down
6 changes: 6 additions & 0 deletions config/config_loader_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ func TestFileGenre(t *testing.T) {
conf := NewLoaderConf(WithPath("../config/testdata/config/properties/application.properties"))
assert.Equal(t, conf.genre, "properties")
}

func TestRootConfig(t *testing.T) {
rc := NewRootConfigBuilder().SetApplication(NewApplicationConfigBuilder().SetName("test-app").Build()).Build()
conf := NewLoaderConf(WithRootConfig(rc))
assert.Equal(t, conf.rc.Application.Name, "test-app")
}
4 changes: 1 addition & 3 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ func registerPOJO() {
}

// Init is to start dubbo-go framework, load local configuration, or read configuration from config-center if necessary.
// finally, for using config-builder to build root config, it would set this root config to global root config pointer,
// allowing global call like GetMetadataConfig works.
// It's deprecated for user to call rootConfig.Init() manually, try config.Load(config.WithRootConfig(rootConfig)) instead.
func (rc *RootConfig) Init() error {
registerPOJO()
if err := rc.Logger.Init(); err != nil { // init default logger
Expand Down Expand Up @@ -191,7 +190,6 @@ func (rc *RootConfig) Init() error {
if err := rc.Consumer.Init(rc); err != nil {
return err
}
rootConfig = rc
// todo if we can remove this from Init in the future?
rc.Start()
return nil
Expand Down

0 comments on commit ebc0db4

Please sign in to comment.