@@ -4,13 +4,14 @@ import (
4
4
"time"
5
5
6
6
"github.com/BurntSushi/toml"
7
+ "github.com/imdario/mergo"
7
8
"github.com/tsingson/discovery/naming"
8
9
"golang.org/x/xerrors"
9
10
10
11
xtime "github.com/tsingson/ex-goim/pkg/time"
11
12
)
12
13
13
- // LogicConfig config.
14
+ // Config config.
14
15
type Config struct {
15
16
Env * Env
16
17
Discovery * naming.Config
@@ -25,6 +26,7 @@ type Config struct {
25
26
Regions map [string ][]string
26
27
}
27
28
29
+ // LogicConfig as alias of Config
28
30
type LogicConfig = Config
29
31
30
32
// Env is env config.
@@ -84,6 +86,8 @@ type Nats struct {
84
86
ChannelID string // "channel-stream"
85
87
AckInbox string // "acks"
86
88
}
89
+
90
+ // NatsConfig as alias of Nats
87
91
type NatsConfig = Nats
88
92
89
93
// RPCClient is RPC client config.
@@ -126,30 +130,32 @@ var (
126
130
127
131
func init () {
128
132
Conf = Default ()
129
- // var (
130
- // defHost, _ = os.Hostname()
131
- // defWeight, _ = strconv.ParseInt(os.Getenv("WEIGHT"), 10, 32)
132
- // )
133
- // flag.StringVar(&confPath, "conf", "logic-example.toml", "default config path")
134
- // flag.StringVar(®ion, "region", os.Getenv("REGION"), "avaliable region. or use REGION env variable, value: sh etc.")
135
- // flag.StringVar(&zone, "zone", os.Getenv("ZONE"), "avaliable zone. or use ZONE env variable, value: sh001/sh002 etc.")
136
- // flag.StringVar(&deployEnv, "deploy.env", os.Getenv("DEPLOY_ENV"), "deploy env. or use DEPLOY_ENV env variable, value: dev/fat1/uat/pre/prod etc.")
137
- // flag.StringVar(&host, "host", defHost, "machine hostname. or use default machine hostname.")
138
- // flag.Int64Var(&weight, "weight", defWeight, "load balancing weight, or use WEIGHT env variable, value: 10 etc.")
133
+
139
134
}
140
135
141
- // Init init config.
142
- func Init (path string ) (cfg * Config , err error ) {
136
+ // Load init config.
137
+ func Load (path string ) (cfg * Config , err error ) {
138
+
139
+ if len (path ) == 0 {
140
+ return cfg , xerrors .New ("config path is nil" )
141
+ }
142
+
143
143
Conf = Default ()
144
- if len (path ) > 0 {
145
- _ , err = toml .DecodeFile (path , & Conf )
146
- } else {
147
- _ , err = toml .DecodeFile (confPath , & Conf )
144
+ cfg = Default ()
145
+
146
+ _ , err = toml .DecodeFile (path , & cfg )
147
+ if err != nil {
148
+ return
148
149
}
150
+ err = mergo .Merge (& Conf , cfg , mergo .WithOverride )
151
+ if err != nil {
152
+ return Conf , err
153
+ }
154
+
149
155
return Conf , nil
150
156
}
151
157
152
- // Init init config.
158
+ // LoadToml init config.
153
159
func LoadToml (path string ) (cfg * Config , err error ) {
154
160
Conf = Default ()
155
161
if len (path ) == 0 {
0 commit comments