@@ -24,6 +24,7 @@ package dynamicconfigfx
24
24
25
25
import (
26
26
"context"
27
+ "path/filepath"
27
28
28
29
"go.uber.org/fx"
29
30
@@ -38,44 +39,70 @@ import (
38
39
// Module provides fx options for dynamic config initialization
39
40
var Module = fx .Options (fx .Provide (New ))
40
41
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
+
41
53
// 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 {
43
55
stopped := make (chan struct {})
44
56
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 {
46
64
close (stopped )
47
65
return nil
48
66
}})
49
67
50
68
var res dynamicconfig.Client
51
69
52
70
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 )
56
74
} else {
57
- switch cfg .DynamicConfig .Client {
75
+ switch p . Cfg .DynamicConfig .Client {
58
76
case dynamicconfig .ConfigStoreClient :
59
- logger .Info ("initialising ConfigStore dynamic config client" )
77
+ p . Logger .Info ("initialising ConfigStore dynamic config client" )
60
78
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 ,
64
82
persistence .DynamicConfig ,
65
83
)
66
84
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 )
69
87
}
70
88
}
71
89
72
90
if res == nil {
73
- logger .Info ("initialising NOP dynamic config client" )
91
+ p . Logger .Info ("initialising NOP dynamic config client" )
74
92
res = dynamicconfig .NewNopClient ()
75
93
} 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 ))
77
95
res = dynamicconfig .NewNopClient ()
78
96
}
79
97
80
98
return res
81
99
}
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
+ }
0 commit comments