2021-01-13 09:06:45 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/imdario/mergo"
|
2021-03-22 08:59:52 +03:00
|
|
|
pbmicro "github.com/unistack-org/micro-config-service/v3/micro"
|
2021-01-16 02:15:17 +03:00
|
|
|
pb "github.com/unistack-org/micro-config-service/v3/proto"
|
2021-01-19 19:29:45 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
2021-01-13 09:06:45 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config"
|
2021-01-19 03:18:11 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2021-01-13 09:06:45 +03:00
|
|
|
)
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
var DefaultStructTag = "service"
|
2021-01-13 09:06:45 +03:00
|
|
|
|
|
|
|
type serviceConfig struct {
|
|
|
|
opts config.Options
|
|
|
|
service string
|
2021-03-22 08:59:52 +03:00
|
|
|
client pbmicro.ConfigClient
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) Init(opts ...config.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:29:45 +03:00
|
|
|
var cli client.Client
|
2021-01-13 09:06:45 +03:00
|
|
|
if c.opts.Context != nil {
|
2021-01-19 19:29:45 +03:00
|
|
|
if v, ok := c.opts.Context.Value(serviceKey{}).(string); ok && v != "" {
|
2021-01-13 09:06:45 +03:00
|
|
|
c.service = v
|
|
|
|
}
|
2021-01-19 19:29:45 +03:00
|
|
|
if v, ok := c.opts.Context.Value(clientKey{}).(client.Client); ok {
|
|
|
|
cli = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli == nil {
|
2021-09-30 02:02:53 +03:00
|
|
|
err := fmt.Errorf("missing client option")
|
|
|
|
c.opts.Logger.Error(c.opts.Context, err)
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.service == "" {
|
2021-09-30 02:02:53 +03:00
|
|
|
err := fmt.Errorf("missing Service option")
|
|
|
|
c.opts.Logger.Error(c.opts.Context, err)
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
2021-03-22 08:59:52 +03:00
|
|
|
c.client = pbmicro.NewConfigClient(c.service, cli)
|
2021-01-19 19:29:45 +03:00
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:32 +03:00
|
|
|
func (c *serviceConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
2021-09-30 02:02:53 +03:00
|
|
|
if err := config.DefaultBeforeLoad(ctx, c); err != nil {
|
|
|
|
return err
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rsp, err := c.client.Load(ctx, &pb.LoadRequest{Service: c.service})
|
2021-09-30 02:02:53 +03:00
|
|
|
if err != nil {
|
|
|
|
c.opts.Logger.Errorf(ctx, "service load error: %v", err)
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("failed to load config: %w", err)
|
|
|
|
}
|
|
|
|
return config.DefaultAfterLoad(ctx, c)
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:32 +03:00
|
|
|
options := config.NewLoadOptions(opts...)
|
|
|
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
|
|
|
if options.Override {
|
|
|
|
mopts = append(mopts, mergo.WithOverride)
|
|
|
|
}
|
|
|
|
if options.Append {
|
|
|
|
mopts = append(mopts, mergo.WithAppendSlice)
|
|
|
|
}
|
|
|
|
|
2021-01-19 03:18:11 +03:00
|
|
|
src, err := rutil.Zero(c.opts.Struct)
|
2021-01-13 09:06:45 +03:00
|
|
|
if err == nil {
|
|
|
|
err = c.opts.Codec.Unmarshal(rsp.Config, src)
|
|
|
|
if err == nil {
|
2021-06-21 16:00:32 +03:00
|
|
|
err = mergo.Merge(c.opts.Struct, src, mopts...)
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
if err != nil {
|
|
|
|
c.opts.Logger.Errorf(ctx, "service load error: %v", err)
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("failed to load config: %w", err)
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
if err := config.DefaultAfterLoad(ctx, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:32 +03:00
|
|
|
func (c *serviceConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
2021-09-30 02:02:53 +03:00
|
|
|
if err := config.DefaultBeforeSave(ctx, c); err != nil {
|
|
|
|
return err
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:47:50 +03:00
|
|
|
buf, err := c.opts.Codec.Marshal(c.opts.Struct)
|
2021-09-30 02:02:53 +03:00
|
|
|
if err == nil {
|
|
|
|
_, err = c.client.Save(ctx, &pb.SaveRequest{Service: c.service, Config: buf})
|
2021-05-09 18:47:50 +03:00
|
|
|
}
|
2021-09-30 02:02:53 +03:00
|
|
|
if err != nil {
|
|
|
|
c.opts.Logger.Errorf(ctx, "service save error: %v", err)
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("failed to save config: %w", err)
|
|
|
|
}
|
2021-05-09 18:47:50 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
if err := config.DefaultAfterSave(ctx, c); err != nil {
|
|
|
|
return err
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) String() string {
|
|
|
|
return "service"
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:25:37 +03:00
|
|
|
func (c *serviceConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
func (c *serviceConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
func NewConfig(opts ...config.Option) config.Config {
|
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &serviceConfig{opts: options}
|
|
|
|
}
|