Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JellyTony committed Mar 13, 2024
1 parent 290636e commit 6e27a53
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 36 deletions.
16 changes: 14 additions & 2 deletions adapter/config/nacos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Option func(*options)
type options struct {
group string
dataID string
format string
}

// WithGroup With nacos config group.
Expand All @@ -32,6 +33,13 @@ func WithDataID(dataID string) Option {
}
}

// WithFormat with nacos config format.
func WithFormat(format string) Option {
return func(o *options) {
o.format = format
}
}

type Config struct {
opts options
client config_client.IConfigClient
Expand All @@ -54,17 +62,21 @@ func (c *Config) Load() ([]*config.KeyValue, error) {
return nil, err
}
k := c.opts.dataID
format := strings.TrimPrefix(filepath.Ext(k), ".")
if format == "" && c.opts.format != "" {
format = c.opts.format
}
return []*config.KeyValue{
{
Key: k,
Value: []byte(content),
Format: strings.TrimPrefix(filepath.Ext(k), "."),
Format: format,
},
}, nil
}

func (c *Config) Watch() (config.Watcher, error) {
watcher := newWatcher(context.Background(), c.opts.dataID, c.opts.group, c.client.CancelListenConfig)
watcher := newWatcher(context.Background(), c.opts.dataID, c.opts.group, c.opts.format, c.client.CancelListenConfig)
err := c.client.ListenConfig(vo.ConfigParam{
DataId: c.opts.dataID,
Group: c.opts.group,
Expand Down
10 changes: 8 additions & 2 deletions adapter/config/nacos/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Watcher struct {
dataID string
group string
format string
content chan string
cancelListenConfig cancelListenConfigFunc

Expand All @@ -22,11 +23,12 @@ type Watcher struct {

type cancelListenConfigFunc func(params vo.ConfigParam) (err error)

func newWatcher(ctx context.Context, dataID string, group string, cancelListenConfig cancelListenConfigFunc) *Watcher {
func newWatcher(ctx context.Context, dataID string, group string, format string, cancelListenConfig cancelListenConfigFunc) *Watcher {
ctx, cancel := context.WithCancel(ctx)
w := &Watcher{
dataID: dataID,
group: group,
format: format,
cancelListenConfig: cancelListenConfig,
content: make(chan string, 100),

Expand All @@ -37,6 +39,10 @@ func newWatcher(ctx context.Context, dataID string, group string, cancelListenCo
}

func (w *Watcher) Next() ([]*config.KeyValue, error) {
format := strings.TrimPrefix(filepath.Ext(w.dataID), ".")
if format == "" && w.format != "" {
format = w.format
}
select {
case <-w.ctx.Done():
return nil, w.ctx.Err()
Expand All @@ -46,7 +52,7 @@ func (w *Watcher) Next() ([]*config.KeyValue, error) {
{
Key: k,
Value: []byte(content),
Format: strings.TrimPrefix(filepath.Ext(k), "."),
Format: format,
},
}, nil
}
Expand Down
68 changes: 39 additions & 29 deletions api/config/v1/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api/config/v1/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ message Nacos {
// nacos config group
string group = 3;
// nacos config namespace
string namespaces = 4;
string namespace = 4;
// nacos config username
string username = 5;
// nacos config password
Expand All @@ -135,6 +135,8 @@ message Nacos {
google.protobuf.Duration timeout = 10;
// nacos logger level. default info
string log_level = 11;
// nacos format, default=yaml
string format = 12;
}

// middleware config
Expand Down
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ func (c *Config) buildNacosSource() ([]kConfig.Source, error) {
} else if cfg.GetLogLevel() == "" {
cfg.LogLevel = "info"
}
if cfg.Format == "" {
cfg.Format = "yaml"
}

clientConfig := constant.NewClientConfig(
constant.WithUsername(cfg.GetUsername()),
constant.WithPassword(cfg.GetPassword()),
constant.WithTimeoutMs(duration),
constant.WithCacheDir(cfg.GetCacheDir()),
constant.WithNamespaceId(cfg.GetNamespaces()),
constant.WithNamespaceId(cfg.GetNamespace()),
constant.WithNotLoadCacheAtStart(true),
constant.WithLogLevel(cfg.LogLevel),
constant.WithOpenKMS(false),
Expand All @@ -131,7 +134,7 @@ func (c *Config) buildNacosSource() ([]kConfig.Source, error) {
}

return []kConfig.Source{
nacos.NewConfigSource(client, nacos.WithDataID(cfg.DataId), nacos.WithGroup(cfg.Group)),
nacos.NewConfigSource(client, nacos.WithDataID(cfg.DataId), nacos.WithGroup(cfg.Group), nacos.WithFormat(cfg.Format)),
}, nil
}

Expand Down

0 comments on commit 6e27a53

Please sign in to comment.