Pass client to more of the runtime
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/micro/go-micro/v2/client"
|
||||
"github.com/micro/go-micro/v2/client/selector"
|
||||
"github.com/micro/go-micro/v2/config"
|
||||
configSrc "github.com/micro/go-micro/v2/config/source"
|
||||
configSrv "github.com/micro/go-micro/v2/config/source/service"
|
||||
"github.com/micro/go-micro/v2/debug/profile"
|
||||
"github.com/micro/go-micro/v2/debug/profile/http"
|
||||
@@ -500,7 +501,6 @@ func (c *cmd) Before(ctx *cli.Context) error {
|
||||
}
|
||||
|
||||
*c.opts.Auth = a()
|
||||
clientOpts = append(clientOpts, client.Auth(*c.opts.Auth))
|
||||
serverOpts = append(serverOpts, server.Auth(*c.opts.Auth))
|
||||
}
|
||||
|
||||
@@ -716,7 +716,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
|
||||
(*c.opts.Auth).Init(authOpts...)
|
||||
|
||||
if ctx.String("config") == "service" {
|
||||
opt := config.WithSource(configSrv.NewSource())
|
||||
opt := config.WithSource(configSrv.NewSource(configSrc.WithClient(*c.opts.Client)))
|
||||
if err := (*c.opts.Config).Init(opt); err != nil {
|
||||
logger.Fatalf("Error configuring config: %v", err)
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package source
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/go-micro/v2/client"
|
||||
"github.com/micro/go-micro/v2/config/encoder"
|
||||
"github.com/micro/go-micro/v2/config/encoder/json"
|
||||
)
|
||||
@@ -13,6 +14,9 @@ type Options struct {
|
||||
|
||||
// for alternative data
|
||||
Context context.Context
|
||||
|
||||
// Client to use for RPC
|
||||
Client client.Client
|
||||
}
|
||||
|
||||
type Option func(o *Options)
|
||||
@@ -21,6 +25,7 @@ func NewOptions(opts ...Option) Options {
|
||||
options := Options{
|
||||
Encoder: json.NewEncoder(),
|
||||
Context: context.Background(),
|
||||
Client: client.DefaultClient,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
@@ -36,3 +41,10 @@ func WithEncoder(e encoder.Encoder) Option {
|
||||
o.Encoder = e
|
||||
}
|
||||
}
|
||||
|
||||
// WithClient sets the source client
|
||||
func WithClient(c client.Client) Option {
|
||||
return func(o *Options) {
|
||||
o.Client = c
|
||||
}
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ type service struct {
|
||||
}
|
||||
|
||||
func (m *service) Read() (set *source.ChangeSet, err error) {
|
||||
client := proto.NewConfigService(m.serviceName, client.DefaultClient)
|
||||
client := proto.NewConfigService(m.serviceName, m.opts.Client)
|
||||
req, err := client.Read(context.Background(), &proto.ReadRequest{
|
||||
Namespace: m.namespace,
|
||||
Path: m.path,
|
||||
@@ -37,7 +37,7 @@ func (m *service) Read() (set *source.ChangeSet, err error) {
|
||||
}
|
||||
|
||||
func (m *service) Watch() (w source.Watcher, err error) {
|
||||
client := proto.NewConfigService(m.serviceName, client.DefaultClient)
|
||||
client := proto.NewConfigService(m.serviceName, m.opts.Client)
|
||||
stream, err := client.Watch(context.Background(), &proto.WatchRequest{
|
||||
Namespace: m.namespace,
|
||||
Path: m.path,
|
||||
@@ -87,6 +87,10 @@ func NewSource(opts ...source.Option) source.Source {
|
||||
}
|
||||
}
|
||||
|
||||
if options.Client == nil {
|
||||
options.Client = client.DefaultClient
|
||||
}
|
||||
|
||||
s := &service{
|
||||
serviceName: addr,
|
||||
opts: options,
|
||||
|
Reference in New Issue
Block a user