Skip to content

Commit 137dca2

Browse files
committed
setup config and missing directories
1 parent 6d2d5c6 commit 137dca2

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

cmd/server/cadence/fx.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ var Module = fx.Options(
4848
type AppParams struct {
4949
fx.In
5050

51-
RootDir string `name:"root-dir"`
5251
Services []string `name:"services"`
5352
AppContext config.Context
5453
Config config.Config
@@ -61,7 +60,6 @@ type AppParams struct {
6160
func NewApp(params AppParams) *App {
6261
app := &App{
6362
cfg: params.Config,
64-
rootDir: params.RootDir,
6563
logger: params.Logger,
6664
services: params.Services,
6765
dynamicConfig: params.DynamicConfig,

common/dynamicconfig/dynamicconfigfx/fx.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package dynamicconfigfx
2424

2525
import (
2626
"context"
27+
"path/filepath"
2728

2829
"go.uber.org/fx"
2930

@@ -38,44 +39,70 @@ import (
3839
// Module provides fx options for dynamic config initialization
3940
var Module = fx.Options(fx.Provide(New))
4041

42+
// Params required to build a new dynamic config.
43+
type Params struct {
44+
fx.In
45+
46+
Cfg config.Config
47+
Logger log.Logger
48+
RootDir string `name:"root-dir"`
49+
50+
Lifecycle fx.Lifecycle
51+
}
52+
4153
// New creates dynamicconfig.Client from the configuration
42-
func New(cfg config.Config, logger log.Logger, lifecycle fx.Lifecycle) dynamicconfig.Client {
54+
func New(p Params) dynamicconfig.Client {
4355
stopped := make(chan struct{})
4456

45-
lifecycle.Append(fx.Hook{OnStop: func(_ context.Context) error {
57+
if p.Cfg.DynamicConfig.Client == "" {
58+
p.Cfg.DynamicConfigClient.Filepath = constructPathIfNeed(p.RootDir, p.Cfg.DynamicConfigClient.Filepath)
59+
} else {
60+
p.Cfg.DynamicConfig.FileBased.Filepath = constructPathIfNeed(p.RootDir, p.Cfg.DynamicConfig.FileBased.Filepath)
61+
}
62+
63+
p.Lifecycle.Append(fx.Hook{OnStop: func(_ context.Context) error {
4664
close(stopped)
4765
return nil
4866
}})
4967

5068
var res dynamicconfig.Client
5169

5270
var err error
53-
if cfg.DynamicConfig.Client == "" {
54-
logger.Warn("falling back to legacy file based dynamicClientConfig")
55-
res, err = dynamicconfig.NewFileBasedClient(&cfg.DynamicConfigClient, logger, stopped)
71+
if p.Cfg.DynamicConfig.Client == "" {
72+
p.Logger.Warn("falling back to legacy file based dynamicClientConfig")
73+
res, err = dynamicconfig.NewFileBasedClient(&p.Cfg.DynamicConfigClient, p.Logger, stopped)
5674
} else {
57-
switch cfg.DynamicConfig.Client {
75+
switch p.Cfg.DynamicConfig.Client {
5876
case dynamicconfig.ConfigStoreClient:
59-
logger.Info("initialising ConfigStore dynamic config client")
77+
p.Logger.Info("initialising ConfigStore dynamic config client")
6078
res, err = configstore.NewConfigStoreClient(
61-
&cfg.DynamicConfig.ConfigStore,
62-
&cfg.Persistence,
63-
logger,
79+
&p.Cfg.DynamicConfig.ConfigStore,
80+
&p.Cfg.Persistence,
81+
p.Logger,
6482
persistence.DynamicConfig,
6583
)
6684
case dynamicconfig.FileBasedClient:
67-
logger.Info("initialising File Based dynamic config client")
68-
res, err = dynamicconfig.NewFileBasedClient(&cfg.DynamicConfig.FileBased, logger, stopped)
85+
p.Logger.Info("initialising File Based dynamic config client")
86+
res, err = dynamicconfig.NewFileBasedClient(&p.Cfg.DynamicConfig.FileBased, p.Logger, stopped)
6987
}
7088
}
7189

7290
if res == nil {
73-
logger.Info("initialising NOP dynamic config client")
91+
p.Logger.Info("initialising NOP dynamic config client")
7492
res = dynamicconfig.NewNopClient()
7593
} else if err != nil {
76-
logger.Error("creating dynamic config client failed, using no-op config client instead", tag.Error(err))
94+
p.Logger.Error("creating dynamic config client failed, using no-op config client instead", tag.Error(err))
7795
res = dynamicconfig.NewNopClient()
7896
}
7997

8098
return res
8199
}
100+
101+
// constructPathIfNeed would append the dir as the root dir
102+
// when the file wasn't absolute path.
103+
func constructPathIfNeed(dir string, file string) string {
104+
if !filepath.IsAbs(file) {
105+
return dir + "/" + file
106+
}
107+
return file
108+
}

common/dynamicconfig/dynamicconfigfx/fx_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,20 @@ import (
3636
func TestModule(t *testing.T) {
3737
app := fxtest.New(t,
3838
testlogger.Module(t),
39-
fx.Provide(func() config.Config { return config.Config{} }),
39+
fx.Provide(func() config.Config { return config.Config{} },
40+
func() fxRoot {
41+
return fxRoot{
42+
RootDir: "../../../",
43+
}
44+
}),
4045
Module,
4146
fx.Invoke(func(c dynamicconfig.Client) {}),
4247
)
4348
app.RequireStart().RequireStop()
4449
}
50+
51+
type fxRoot struct {
52+
fx.Out
53+
54+
RootDir string `name:"root-dir"`
55+
}

0 commit comments

Comments
 (0)