Skip to content

Commit 8edadf8

Browse files
author
coder2z
committed
配置优化
1 parent c66936b commit 8edadf8

File tree

14 files changed

+618
-35
lines changed

14 files changed

+618
-35
lines changed

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ go 1.16
44

55
require (
66
github.com/davecgh/go-spew v1.1.1
7+
github.com/dustin/go-humanize v1.0.0 // indirect
78
github.com/go-playground/locales v0.13.0
89
github.com/go-playground/universal-translator v0.17.0
910
github.com/go-playground/validator/v10 v10.4.1
11+
github.com/google/uuid v1.3.0 // indirect
1012
github.com/json-iterator/go v1.1.10
1113
github.com/mitchellh/mapstructure v1.1.2
1214
github.com/modern-go/reflect2 v1.0.1
15+
github.com/pelletier/go-toml v1.2.0
16+
github.com/philchia/agollo/v4 v4.1.4
1317
github.com/pkg/errors v0.8.1
1418
github.com/robfig/cron/v3 v3.0.1
1519
github.com/spf13/cast v1.3.1
1620
github.com/spf13/cobra v1.1.3
17-
go.uber.org/zap v1.10.0
21+
go.etcd.io/etcd v0.5.0-alpha.5.0.20200425165423-262c93980547
22+
go.uber.org/zap v1.14.1
1823
golang.org/x/sync v0.0.0-20190423024810-112230192c58
24+
sigs.k8s.io/yaml v1.3.0 // indirect
1925
)

go.sum

Lines changed: 98 additions & 4 deletions
Large diffs are not rendered by default.

xcfg/api.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package xcfg
22

33
import (
4+
"github.com/pelletier/go-toml"
45
"io"
56

67
"github.com/davecgh/go-spew/spew"
@@ -16,6 +17,8 @@ type DataSource interface {
1617
// Unmarshaler ...
1718
type Unmarshaler func([]byte, interface{}) error
1819

20+
var DefaultConfigUnmarshaler = toml.Unmarshal
21+
1922
var defaultConfiguration = New()
2023

2124
// OnChange 注册change回调函数
@@ -30,7 +33,18 @@ func LoadFromDataSource(ds DataSource, unmarshaler Unmarshaler) error {
3033
return defaultConfiguration.LoadFromDataSource(ds, unmarshaler)
3134
}
3235

33-
// Load loads configuration from provided provider with default defaultConfiguration.
36+
// LoadFromConfigAddr load configuration from configAddr
37+
// eg:apollo://ip:port?appId=XXX&cluster=XXX&namespaceName=XXX&key=XXX&accesskeySecret=XXX&insecureSkipVerify=XXX&cacheDir=XXX
38+
// etcd://ip:port?username=XXX&password=XXX&key=key
39+
func LoadFromConfigAddr(configAddr string) error {
40+
source, err := NewDataSource(configAddr)
41+
if err != nil {
42+
return err
43+
}
44+
return LoadFromDataSource(source, DefaultConfigUnmarshaler)
45+
}
46+
47+
// LoadFromReader Load loads configuration from provided provider with default defaultConfiguration.
3448
func LoadFromReader(r io.Reader, unmarshaler Unmarshaler) error {
3549
return defaultConfiguration.LoadFromReader(r, unmarshaler)
3650
}

xcfg/apollo/apollo.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package apollo
2+
3+
import (
4+
"github.com/coder2z/g-saber/xcfg"
5+
"github.com/philchia/agollo/v4"
6+
)
7+
8+
type apolloDataSource struct {
9+
client agollo.Client
10+
namespace string
11+
propertyKey string
12+
changed chan struct{}
13+
}
14+
15+
// NewDataSource creates an apolloDataSource
16+
func NewDataSource(conf *agollo.Conf, namespace string, key string, watch bool) xcfg.DataSource {
17+
client := agollo.NewClient(conf, agollo.WithLogger(&agolloLogger{}))
18+
ap := &apolloDataSource{
19+
client: client,
20+
namespace: namespace,
21+
propertyKey: key,
22+
changed: make(chan struct{}, 1),
23+
}
24+
_ = ap.client.Start()
25+
if watch {
26+
ap.client.OnUpdate(
27+
func(event *agollo.ChangeEvent) {
28+
ap.changed <- struct{}{}
29+
})
30+
}
31+
return ap
32+
}
33+
34+
// ReadConfig reads xcfg content from apollo
35+
func (ap *apolloDataSource) ReadConfig() ([]byte, error) {
36+
value := ap.client.GetString(ap.propertyKey, agollo.WithNamespace(ap.namespace))
37+
return []byte(value), nil
38+
}
39+
40+
// IsConfigChanged returns a chanel for notification when the xcfg changed
41+
func (ap *apolloDataSource) IsConfigChanged() <-chan struct{} {
42+
return ap.changed
43+
}
44+
45+
// Close stops watching the xcfg changed
46+
func (ap *apolloDataSource) Close() error {
47+
_ = ap.client.Stop()
48+
close(ap.changed)
49+
return nil
50+
}
51+
52+
type agolloLogger struct {
53+
}
54+
55+
// Infof ...
56+
func (l *agolloLogger) Infof(format string, args ...interface{}) {
57+
}
58+
59+
// Errorf ...
60+
func (l *agolloLogger) Errorf(format string, args ...interface{}) {
61+
}

xcfg/apollo/register.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package apollo
2+
3+
import (
4+
"github.com/coder2z/g-saber/xcfg"
5+
"github.com/philchia/agollo/v4"
6+
"github.com/spf13/cast"
7+
"net/url"
8+
)
9+
10+
func init() {
11+
xcfg.Register(new(apollo))
12+
}
13+
14+
// DataSourceApollo defines apollo scheme
15+
const DataSourceApollo = "apollo"
16+
17+
type apollo struct{}
18+
19+
func (e apollo) Register() (string, xcfg.DataSourceCreatorFunc) {
20+
return DataSourceApollo, func(addr string) xcfg.DataSource {
21+
var watch bool
22+
if addr == "" {
23+
return nil
24+
}
25+
// configAddr is a string in this format:
26+
// apollo://ip:port?appId=XXX&cluster=XXX&namespaceName=XXX&key=XXX&accesskeySecret=XXX&insecureSkipVerify=XXX&cacheDir=XXX&watch=true
27+
urlObj, err := url.Parse(addr)
28+
if err != nil {
29+
return nil
30+
}
31+
watch = cast.ToBool(urlObj.Query().Get("watch"))
32+
apolloConf := agollo.Conf{
33+
AppID: urlObj.Query().Get("appId"),
34+
Cluster: urlObj.Query().Get("cluster"),
35+
NameSpaceNames: []string{urlObj.Query().Get("namespaceName")},
36+
MetaAddr: urlObj.Host,
37+
InsecureSkipVerify: cast.ToBool(urlObj.Query().Get("insecureSkipVerify")),
38+
AccesskeySecret: urlObj.Query().Get("accesskeySecret"),
39+
CacheDir: ".",
40+
}
41+
if urlObj.Query().Get("cacheDir") != "" {
42+
apolloConf.CacheDir = urlObj.Query().Get("cacheDir")
43+
}
44+
return NewDataSource(&apolloConf, urlObj.Query().Get("namespaceName"), urlObj.Query().Get("key"), watch)
45+
}
46+
}

xcfg/config.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package xcfg
33
import (
44
"errors"
55
"fmt"
6-
xcast2 "github.com/coder2z/g-saber/xtransform/xcast"
6+
"github.com/coder2z/g-saber/xstring"
7+
"github.com/coder2z/g-saber/xtransform/xcast"
78
"io"
89
"io/ioutil"
910
"reflect"
@@ -94,13 +95,15 @@ func (c *Configuration) LoadFromDataSource(ds DataSource, unmarshaler Unmarshale
9495
// Load ...
9596
func (c *Configuration) Load(content []byte, unmarshaler Unmarshaler) error {
9697
configuration := make(map[string]interface{})
97-
if err := unmarshaler(content, &configuration); err != nil {
98+
// 替换里面的环境变量 ....
99+
envContent := xstring.ExpandEnv(xcast.ToString(content))
100+
if err := unmarshaler([]byte(envContent), &configuration); err != nil {
98101
return err
99102
}
100103
return c.apply(configuration)
101104
}
102105

103-
// Load loads configuration from provided data source.
106+
// LoadFromReader loads configuration from provided data source.
104107
func (c *Configuration) LoadFromReader(reader io.Reader, unmarshaler Unmarshaler) error {
105108
content, err := ioutil.ReadAll(reader)
106109
if err != nil {
@@ -189,7 +192,7 @@ func GetString(key string) string {
189192

190193
// GetString returns the value associated with the key as a string.
191194
func (c *Configuration) GetString(key string) string {
192-
return xcast2.ToString(c.Get(key))
195+
return xcast.ToString(c.Get(key))
193196
}
194197

195198
// GetBool returns the value associated with the key as a boolean with default defaultConfiguration.
@@ -199,7 +202,7 @@ func GetBool(key string) bool {
199202

200203
// GetBool returns the value associated with the key as a boolean.
201204
func (c *Configuration) GetBool(key string) bool {
202-
return xcast2.ToBool(c.Get(key))
205+
return xcast.ToBool(c.Get(key))
203206
}
204207

205208
// GetInt returns the value associated with the key as an integer with default defaultConfiguration.
@@ -209,7 +212,7 @@ func GetInt(key string) int {
209212

210213
// GetInt returns the value associated with the key as an integer.
211214
func (c *Configuration) GetInt(key string) int {
212-
return xcast2.ToInt(c.Get(key))
215+
return xcast.ToInt(c.Get(key))
213216
}
214217

215218
// GetInt64 returns the value associated with the key as an integer with default defaultConfiguration.
@@ -219,7 +222,7 @@ func GetInt64(key string) int64 {
219222

220223
// GetInt64 returns the value associated with the key as an integer.
221224
func (c *Configuration) GetInt64(key string) int64 {
222-
return xcast2.ToInt64(c.Get(key))
225+
return xcast.ToInt64(c.Get(key))
223226
}
224227

225228
// GetFloat64 returns the value associated with the key as a float64 with default defaultConfiguration.
@@ -229,7 +232,7 @@ func GetFloat64(key string) float64 {
229232

230233
// GetFloat64 returns the value associated with the key as a float64.
231234
func (c *Configuration) GetFloat64(key string) float64 {
232-
return xcast2.ToFloat64(c.Get(key))
235+
return xcast.ToFloat64(c.Get(key))
233236
}
234237

235238
// GetTime returns the value associated with the key as time with default defaultConfiguration.
@@ -239,7 +242,7 @@ func GetTime(key string) time.Time {
239242

240243
// GetTime returns the value associated with the key as time.
241244
func (c *Configuration) GetTime(key string) time.Time {
242-
return xcast2.ToTime(c.Get(key))
245+
return xcast.ToTime(c.Get(key))
243246
}
244247

245248
// GetDuration returns the value associated with the key as a duration with default defaultConfiguration.
@@ -249,7 +252,7 @@ func GetDuration(key string) time.Duration {
249252

250253
// GetDuration returns the value associated with the key as a duration.
251254
func (c *Configuration) GetDuration(key string) time.Duration {
252-
return xcast2.ToDuration(c.Get(key))
255+
return xcast.ToDuration(c.Get(key))
253256
}
254257

255258
// GetStringSlice returns the value associated with the key as a slice of strings with default defaultConfiguration.
@@ -259,7 +262,7 @@ func GetStringSlice(key string) []string {
259262

260263
// GetStringSlice returns the value associated with the key as a slice of strings.
261264
func (c *Configuration) GetStringSlice(key string) []string {
262-
return xcast2.ToStringSlice(c.Get(key))
265+
return xcast.ToStringSlice(c.Get(key))
263266
}
264267

265268
// GetSlice returns the value associated with the key as a slice of strings with default defaultConfiguration.
@@ -269,7 +272,7 @@ func GetSlice(key string) []interface{} {
269272

270273
// GetSlice returns the value associated with the key as a slice of strings.
271274
func (c *Configuration) GetSlice(key string) []interface{} {
272-
return xcast2.ToSlice(c.Get(key))
275+
return xcast.ToSlice(c.Get(key))
273276
}
274277

275278
// GetStringMap returns the value associated with the key as a map of interfaces with default defaultConfiguration.
@@ -279,7 +282,7 @@ func GetStringMap(key string) map[string]interface{} {
279282

280283
// GetStringMap returns the value associated with the key as a map of interfaces.
281284
func (c *Configuration) GetStringMap(key string) map[string]interface{} {
282-
return xcast2.ToStringMap(c.Get(key))
285+
return xcast.ToStringMap(c.Get(key))
283286
}
284287

285288
// GetStringMapString returns the value associated with the key as a map of strings with default defaultConfiguration.
@@ -289,12 +292,12 @@ func GetStringMapString(key string) map[string]string {
289292

290293
// GetStringMapString returns the value associated with the key as a map of strings.
291294
func (c *Configuration) GetStringMapString(key string) map[string]string {
292-
return xcast2.ToStringMapString(c.Get(key))
295+
return xcast.ToStringMapString(c.Get(key))
293296
}
294297

295298
// GetSliceStringMap returns the value associated with the slice of maps.
296299
func (c *Configuration) GetSliceStringMap(key string) []map[string]interface{} {
297-
return xcast2.ToSliceStringMap(c.Get(key))
300+
return xcast.ToSliceStringMap(c.Get(key))
298301
}
299302

300303
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings with default defaultConfiguration.
@@ -304,7 +307,7 @@ func GetStringMapStringSlice(key string) map[string][]string {
304307

305308
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
306309
func (c *Configuration) GetStringMapStringSlice(key string) map[string][]string {
307-
return xcast2.ToStringMapStringSlice(c.Get(key))
310+
return xcast.ToStringMapStringSlice(c.Get(key))
308311
}
309312

310313
// UnmarshalWithExpect unmarshal key, returns expect if failed
@@ -380,7 +383,7 @@ func lookup(prefix string, target map[string]interface{}, data map[string]interf
380383
if prefix == "" {
381384
pp = k
382385
}
383-
if dd, err := xcast2.ToStringMapE(v); err == nil {
386+
if dd, err := xcast.ToStringMapE(v); err == nil {
384387
lookup(pp, dd, data, sep)
385388
} else {
386389
data[pp] = v

0 commit comments

Comments
 (0)