1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-05-19 21:23:04 +02:00

80 lines
1.6 KiB
Go
Raw Normal View History

2020-12-26 15:32:45 +00:00
package mucp
import (
"context"
2021-01-20 21:01:10 +00:00
"github.com/asim/go-micro/v3/cmd"
"github.com/asim/go-micro/v3/config/source"
log "github.com/asim/go-micro/v3/logger"
proto "github.com/asim/go-micro/plugins/config/source/mucp/v3/proto"
2020-12-26 15:32:45 +00:00
)
var (
DefaultPath = "/micro/config"
DefaultServiceName = "go.micro.config"
)
type mucpSource struct {
serviceName string
path string
opts source.Options
client proto.SourceService
}
func (m *mucpSource) Read() (set *source.ChangeSet, err error) {
req, err := m.client.Read(context.Background(), &proto.ReadRequest{Path: m.path})
if err != nil {
return nil, err
}
return toChangeSet(req.ChangeSet), nil
}
func (m *mucpSource) Watch() (w source.Watcher, err error) {
stream, err := m.client.Watch(context.Background(), &proto.WatchRequest{Path: m.path})
if err != nil {
log.Error("watch err: ", err)
return
}
return newWatcher(stream)
}
// Write is unsupported
func (m *mucpSource) Write(cs *source.ChangeSet) error {
return nil
}
func (m *mucpSource) String() string {
return "mucp"
}
func NewSource(opts ...source.Option) source.Source {
var options source.Options
for _, o := range opts {
o(&options)
}
addr := DefaultServiceName
path := DefaultPath
if options.Context != nil {
a, ok := options.Context.Value(serviceNameKey{}).(string)
if ok {
addr = a
}
p, ok := options.Context.Value(pathKey{}).(string)
if ok {
path = p
}
}
s := &mucpSource{
serviceName: addr,
path: path,
opts: options,
client: proto.NewSourceService(addr, *cmd.DefaultOptions().Client),
}
return s
}