2023-05-05 17:36:54 +03:00
|
|
|
package consul // import "go.unistack.org/micro-config-consul/v4"
|
2020-12-08 00:58:12 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-13 19:08:09 +03:00
|
|
|
"fmt"
|
2020-12-08 00:58:12 +03:00
|
|
|
|
2020-12-09 13:15:36 +03:00
|
|
|
"github.com/hashicorp/consul/api"
|
2020-12-20 00:31:02 +03:00
|
|
|
"github.com/imdario/mergo"
|
2023-05-05 17:36:54 +03:00
|
|
|
"go.unistack.org/micro/v4/config"
|
2023-08-14 23:36:40 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-05-05 17:36:54 +03:00
|
|
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
2020-12-08 00:58:12 +03:00
|
|
|
)
|
|
|
|
|
2021-09-30 00:39:49 +03:00
|
|
|
var DefaultStructTag = "consul"
|
2020-12-08 00:58:12 +03:00
|
|
|
|
|
|
|
type consulConfig struct {
|
|
|
|
opts config.Options
|
|
|
|
cli *api.Client
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *consulConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:36:40 +03:00
|
|
|
func (c *consulConfig) Init(opts ...options.Option) error {
|
|
|
|
var err error
|
2020-12-08 00:58:12 +03:00
|
|
|
for _, o := range opts {
|
2023-08-14 23:36:40 +03:00
|
|
|
if err = o(&c.opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:41:44 +03:00
|
|
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.opts.Codec == nil && !c.opts.AllowFail {
|
2020-12-13 13:30:00 +03:00
|
|
|
return config.ErrCodecMissing
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:15:36 +03:00
|
|
|
cfg := api.DefaultConfigWithLogger(&consulLogger{logger: c.opts.Logger})
|
2020-12-08 00:58:12 +03:00
|
|
|
path := ""
|
|
|
|
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
if v, ok := c.opts.Context.Value(configKey{}).(*api.Config); ok {
|
2020-12-09 13:15:36 +03:00
|
|
|
cfg.Address = v.Address
|
|
|
|
cfg.Scheme = v.Scheme
|
|
|
|
cfg.Datacenter = v.Datacenter
|
|
|
|
cfg.Transport = v.Transport
|
|
|
|
cfg.HttpClient = v.HttpClient
|
|
|
|
cfg.HttpAuth = v.HttpAuth
|
|
|
|
cfg.WaitTime = v.WaitTime
|
|
|
|
cfg.Token = v.Token
|
|
|
|
cfg.TokenFile = v.TokenFile
|
|
|
|
cfg.Namespace = v.Namespace
|
|
|
|
cfg.TLSConfig = v.TLSConfig
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(addrKey{}).(string); ok {
|
|
|
|
cfg.Address = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(tokenKey{}).(string); ok {
|
|
|
|
cfg.Token = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
|
|
|
path = v
|
|
|
|
}
|
2020-12-09 13:21:48 +03:00
|
|
|
/*
|
|
|
|
if v, ok := c.opts.Context.Value(tlsConfigKey{}).(*tls.Config); ok {
|
|
|
|
cfg.TLSConfig = *v
|
|
|
|
}
|
|
|
|
*/
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := api.NewClient(cfg)
|
2023-03-14 13:41:44 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c.cli = cli
|
|
|
|
c.path = path
|
|
|
|
|
2023-03-14 13:41:44 +03:00
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-03-14 10:53:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:58:12 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:36:40 +03:00
|
|
|
func (c *consulConfig) Load(ctx context.Context, opts ...options.Option) error {
|
2024-01-15 01:15:20 +03:00
|
|
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-18 16:05:44 +03:00
|
|
|
options := config.NewLoadOptions(opts...)
|
2023-03-14 13:41:44 +03:00
|
|
|
|
|
|
|
if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := c.path
|
|
|
|
|
2021-11-18 16:05:44 +03:00
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pair, _, err := c.cli.KV().Get(path, nil)
|
2021-09-30 00:39:49 +03:00
|
|
|
if err != nil {
|
2021-11-18 16:05:44 +03:00
|
|
|
err = fmt.Errorf("consul path %s load error: %w", path, err)
|
2021-09-30 00:39:49 +03:00
|
|
|
} else if pair == nil {
|
2021-11-18 16:05:44 +03:00
|
|
|
err = fmt.Errorf("consul path %s load error: not found", path)
|
2021-09-30 00:39:49 +03:00
|
|
|
}
|
2021-06-21 16:03:21 +03:00
|
|
|
|
2023-03-14 13:41:44 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2021-09-30 00:39:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
|
|
|
if options.Override {
|
|
|
|
mopts = append(mopts, mergo.WithOverride)
|
|
|
|
}
|
|
|
|
if options.Append {
|
|
|
|
mopts = append(mopts, mergo.WithAppendSlice)
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
2021-08-04 01:38:44 +03:00
|
|
|
|
2021-09-30 00:39:49 +03:00
|
|
|
src, err := rutil.Zero(dst)
|
|
|
|
if err == nil {
|
|
|
|
err = c.opts.Codec.Unmarshal(pair.Value, src)
|
2020-12-20 00:31:02 +03:00
|
|
|
if err == nil {
|
2021-09-30 00:39:49 +03:00
|
|
|
err = mergo.Merge(dst, src, mopts...)
|
2020-12-20 00:31:02 +03:00
|
|
|
}
|
2021-09-30 00:39:49 +03:00
|
|
|
}
|
2020-12-20 00:31:02 +03:00
|
|
|
|
2023-03-14 13:41:44 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-18 03:39:35 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:41:44 +03:00
|
|
|
if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:28:03 +03:00
|
|
|
return err
|
2020-12-18 03:39:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
2023-08-14 23:36:40 +03:00
|
|
|
func (c *consulConfig) Save(ctx context.Context, opts ...options.Option) error {
|
2024-01-15 01:15:20 +03:00
|
|
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-18 16:05:44 +03:00
|
|
|
options := config.NewSaveOptions(opts...)
|
2023-03-14 13:41:44 +03:00
|
|
|
|
|
|
|
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := c.path
|
|
|
|
|
2023-03-15 00:03:33 +03:00
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
2021-11-18 16:05:44 +03:00
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:03:33 +03:00
|
|
|
buf, err := c.opts.Codec.Marshal(dst)
|
2021-05-08 17:07:32 +03:00
|
|
|
if err == nil {
|
2021-11-18 16:05:44 +03:00
|
|
|
_, err = c.cli.KV().Put(&api.KVPair{Key: path, Value: buf}, nil)
|
2021-05-08 17:07:32 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:41:44 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("consul path %s save error: %v", path, err)
|
2021-05-08 17:07:32 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:41:44 +03:00
|
|
|
if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:28:03 +03:00
|
|
|
return err
|
2020-12-18 03:39:35 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:58:12 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *consulConfig) String() string {
|
|
|
|
return "consul"
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:20:57 +03:00
|
|
|
func (c *consulConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:36:40 +03:00
|
|
|
func (c *consulConfig) Watch(ctx context.Context, opts ...options.Option) (config.Watcher, error) {
|
2021-11-18 16:05:44 +03:00
|
|
|
path := c.path
|
|
|
|
options := config.NewWatchOptions(opts...)
|
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 01:38:44 +03:00
|
|
|
w := &consulWatcher{
|
|
|
|
cli: c.cli,
|
2021-11-18 16:05:44 +03:00
|
|
|
path: path,
|
2021-08-04 01:38:44 +03:00
|
|
|
opts: c.opts,
|
|
|
|
wopts: config.NewWatchOptions(opts...),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
vchan: make(chan map[string]interface{}),
|
|
|
|
echan: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
go w.run()
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:36:40 +03:00
|
|
|
func NewConfig(opts ...options.Option) config.Config {
|
2020-12-08 00:58:12 +03:00
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &consulConfig{opts: options}
|
|
|
|
}
|