2020-01-17 23:53:33 +08:00
|
|
|
package service
|
2020-01-17 00:10:15 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-27 12:12:34 +01:00
|
|
|
"net/http"
|
2020-01-17 00:10:15 +08:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/config/source"
|
|
|
|
proto "github.com/micro/go-micro/v2/config/source/service/proto"
|
2020-05-27 12:12:34 +01:00
|
|
|
"github.com/micro/go-micro/v2/errors"
|
2020-03-11 20:55:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-01-17 00:10:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-03-11 22:31:24 +00:00
|
|
|
DefaultName = "go.micro.config"
|
2020-07-07 08:19:52 +01:00
|
|
|
DefaultNamespace = "micro"
|
2020-03-11 22:31:24 +00:00
|
|
|
DefaultPath = ""
|
2020-01-17 00:10:15 +08:00
|
|
|
)
|
|
|
|
|
2020-01-17 23:27:41 +08:00
|
|
|
type service struct {
|
2020-01-17 00:10:15 +08:00
|
|
|
serviceName string
|
2020-03-11 22:31:24 +00:00
|
|
|
namespace string
|
2020-01-20 18:31:18 +08:00
|
|
|
path string
|
2020-01-17 00:10:15 +08:00
|
|
|
opts source.Options
|
2020-01-23 11:37:54 +00:00
|
|
|
client proto.ConfigService
|
2020-01-17 00:10:15 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:27:41 +08:00
|
|
|
func (m *service) Read() (set *source.ChangeSet, err error) {
|
2020-05-11 17:57:39 +01:00
|
|
|
client := proto.NewConfigService(m.serviceName, m.opts.Client)
|
2020-03-12 18:13:03 +00:00
|
|
|
req, err := client.Read(context.Background(), &proto.ReadRequest{
|
2020-03-11 22:31:24 +00:00
|
|
|
Namespace: m.namespace,
|
|
|
|
Path: m.path,
|
|
|
|
})
|
2020-05-27 12:12:34 +01:00
|
|
|
if verr, ok := err.(*errors.Error); ok && verr.Code == http.StatusNotFound {
|
2020-05-27 12:20:31 +01:00
|
|
|
return nil, nil
|
2020-05-27 12:12:34 +01:00
|
|
|
} else if err != nil {
|
2020-01-17 00:10:15 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toChangeSet(req.Change.ChangeSet), nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:27:41 +08:00
|
|
|
func (m *service) Watch() (w source.Watcher, err error) {
|
2020-05-11 17:57:39 +01:00
|
|
|
client := proto.NewConfigService(m.serviceName, m.opts.Client)
|
2020-03-12 18:13:03 +00:00
|
|
|
stream, err := client.Watch(context.Background(), &proto.WatchRequest{
|
2020-03-11 22:31:24 +00:00
|
|
|
Namespace: m.namespace,
|
|
|
|
Path: m.path,
|
|
|
|
})
|
2020-01-17 00:10:15 +08:00
|
|
|
if err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error("watch err: ", err)
|
|
|
|
}
|
2020-01-17 00:10:15 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return newWatcher(stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write is unsupported
|
2020-01-17 23:27:41 +08:00
|
|
|
func (m *service) Write(cs *source.ChangeSet) error {
|
2020-01-17 00:10:15 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:27:41 +08:00
|
|
|
func (m *service) String() string {
|
2020-01-18 23:16:23 +08:00
|
|
|
return "service"
|
2020-01-17 00:10:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSource(opts ...source.Option) source.Source {
|
|
|
|
var options source.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:16:23 +08:00
|
|
|
addr := DefaultName
|
2020-03-11 22:31:24 +00:00
|
|
|
namespace := DefaultNamespace
|
2020-01-20 18:31:18 +08:00
|
|
|
path := DefaultPath
|
2020-01-17 00:10:15 +08:00
|
|
|
|
|
|
|
if options.Context != nil {
|
|
|
|
a, ok := options.Context.Value(serviceNameKey{}).(string)
|
|
|
|
if ok {
|
|
|
|
addr = a
|
|
|
|
}
|
2020-01-20 18:31:18 +08:00
|
|
|
|
2020-03-11 22:31:24 +00:00
|
|
|
k, ok := options.Context.Value(namespaceKey{}).(string)
|
2020-01-20 18:31:18 +08:00
|
|
|
if ok {
|
2020-03-11 22:31:24 +00:00
|
|
|
namespace = k
|
2020-01-20 18:31:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p, ok := options.Context.Value(pathKey{}).(string)
|
|
|
|
if ok {
|
|
|
|
path = p
|
|
|
|
}
|
2020-01-17 00:10:15 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 17:57:39 +01:00
|
|
|
if options.Client == nil {
|
|
|
|
options.Client = client.DefaultClient
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:27:41 +08:00
|
|
|
s := &service{
|
2020-01-17 00:10:15 +08:00
|
|
|
serviceName: addr,
|
|
|
|
opts: options,
|
2020-03-11 22:31:24 +00:00
|
|
|
namespace: namespace,
|
2020-01-20 18:31:18 +08:00
|
|
|
path: path,
|
2020-01-17 00:10:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|