Pass client to more of the runtime

This commit is contained in:
Ben Toogood 2020-05-11 17:57:39 +01:00
parent f892b41299
commit efb64b7dbb
10 changed files with 56 additions and 20 deletions

View File

@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/micro/go-micro/v2/auth/provider" "github.com/micro/go-micro/v2/auth/provider"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/store"
) )
@ -12,10 +13,12 @@ func NewOptions(opts ...Option) Options {
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
} }
if len(options.Namespace) == 0 { if len(options.Namespace) == 0 {
options.Namespace = DefaultNamespace options.Namespace = DefaultNamespace
} }
if options.Client == nil {
options.Client = client.DefaultClient
}
return options return options
} }
@ -39,6 +42,8 @@ type Options struct {
LoginURL string LoginURL string
// Store to back auth // Store to back auth
Store store.Store Store store.Store
// Client to use for RPC
Client client.Client
} }
type Option func(o *Options) type Option func(o *Options)
@ -100,6 +105,13 @@ func LoginURL(url string) Option {
} }
} }
// WithClient sets the client to use when making requests
func WithClient(c client.Client) Option {
return func(o *Options) {
o.Client = c
}
}
type GenerateOptions struct { type GenerateOptions struct {
// Metadata associated with the account // Metadata associated with the account
Metadata map[string]string Metadata map[string]string

View File

@ -42,9 +42,11 @@ func (s *svc) Init(opts ...auth.Option) {
o(&s.options) o(&s.options)
} }
dc := client.DefaultClient if s.options.Client == nil {
s.auth = pb.NewAuthService("go.micro.auth", dc) s.options.Client = client.DefaultClient
s.rule = pb.NewRulesService("go.micro.auth", dc) }
s.auth = pb.NewAuthService("go.micro.auth", s.options.Client)
s.rule = pb.NewRulesService("go.micro.auth", s.options.Client)
// if we have a JWT public key passed as an option, // if we have a JWT public key passed as an option,
// we can decode tokens with the type "JWT" locally // we can decode tokens with the type "JWT" locally

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"time" "time"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/codec" "github.com/micro/go-micro/v2/codec"
@ -17,7 +16,6 @@ type Options struct {
ContentType string ContentType string
// Plugged interfaces // Plugged interfaces
Auth auth.Auth
Broker broker.Broker Broker broker.Broker
Codecs map[string]codec.NewCodec Codecs map[string]codec.NewCodec
Registry registry.Registry Registry registry.Registry
@ -105,7 +103,6 @@ func NewOptions(options ...Option) Options {
}, },
PoolSize: DefaultPoolSize, PoolSize: DefaultPoolSize,
PoolTTL: DefaultPoolTTL, PoolTTL: DefaultPoolTTL,
Auth: auth.DefaultAuth,
Broker: broker.DefaultBroker, Broker: broker.DefaultBroker,
Selector: selector.DefaultSelector, Selector: selector.DefaultSelector,
Registry: registry.DefaultRegistry, Registry: registry.DefaultRegistry,
@ -126,13 +123,6 @@ func Broker(b broker.Broker) Option {
} }
} }
// Auth to be used when making a request
func Auth(a auth.Auth) Option {
return func(o *Options) {
o.Auth = a
}
}
// Codec to be used to encode/decode requests for a given content type // Codec to be used to encode/decode requests for a given content type
func Codec(contentType string, c codec.NewCodec) Option { func Codec(contentType string, c codec.NewCodec) Option {
return func(o *Options) { return func(o *Options) {

View File

@ -13,6 +13,7 @@ import (
"github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config" "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" 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"
"github.com/micro/go-micro/v2/debug/profile/http" "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() *c.opts.Auth = a()
clientOpts = append(clientOpts, client.Auth(*c.opts.Auth))
serverOpts = append(serverOpts, server.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...) (*c.opts.Auth).Init(authOpts...)
if ctx.String("config") == "service" { 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 { if err := (*c.opts.Config).Init(opt); err != nil {
logger.Fatalf("Error configuring config: %v", err) logger.Fatalf("Error configuring config: %v", err)
} }

View File

@ -3,6 +3,7 @@ package source
import ( import (
"context" "context"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/config/encoder" "github.com/micro/go-micro/v2/config/encoder"
"github.com/micro/go-micro/v2/config/encoder/json" "github.com/micro/go-micro/v2/config/encoder/json"
) )
@ -13,6 +14,9 @@ type Options struct {
// for alternative data // for alternative data
Context context.Context Context context.Context
// Client to use for RPC
Client client.Client
} }
type Option func(o *Options) type Option func(o *Options)
@ -21,6 +25,7 @@ func NewOptions(opts ...Option) Options {
options := Options{ options := Options{
Encoder: json.NewEncoder(), Encoder: json.NewEncoder(),
Context: context.Background(), Context: context.Background(),
Client: client.DefaultClient,
} }
for _, o := range opts { for _, o := range opts {
@ -36,3 +41,10 @@ func WithEncoder(e encoder.Encoder) Option {
o.Encoder = e o.Encoder = e
} }
} }
// WithClient sets the source client
func WithClient(c client.Client) Option {
return func(o *Options) {
o.Client = c
}
}

View File

@ -24,7 +24,7 @@ type service struct {
} }
func (m *service) Read() (set *source.ChangeSet, err error) { 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{ req, err := client.Read(context.Background(), &proto.ReadRequest{
Namespace: m.namespace, Namespace: m.namespace,
Path: m.path, 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) { 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{ stream, err := client.Watch(context.Background(), &proto.WatchRequest{
Namespace: m.namespace, Namespace: m.namespace,
Path: m.path, Path: m.path,
@ -87,6 +87,10 @@ func NewSource(opts ...source.Option) source.Source {
} }
} }
if options.Client == nil {
options.Client = client.DefaultClient
}
s := &service{ s := &service{
serviceName: addr, serviceName: addr,
opts: options, opts: options,

View File

@ -155,7 +155,6 @@ func Tracer(t trace.Tracer) Option {
func Auth(a auth.Auth) Option { func Auth(a auth.Auth) Option {
return func(o *Options) { return func(o *Options) {
o.Auth = a o.Auth = a
o.Client.Init(client.Auth(a))
o.Server.Init(server.Auth(a)) o.Server.Init(server.Auth(a))
} }
} }

View File

@ -115,7 +115,9 @@ func (s *service) Init(opts ...Option) {
s.opts.Store.Init(store.Table(name)) s.opts.Store.Init(store.Table(name))
// Set the client for the micro clients // Set the client for the micro clients
s.opts.Auth.Init(auth.WithClient(s.Client()))
s.opts.Runtime.Init(runtime.WithClient(s.Client())) s.opts.Runtime.Init(runtime.WithClient(s.Client()))
s.opts.Store.Init(store.WithClient(s.Client()))
}) })
} }

View File

@ -3,6 +3,8 @@ package store
import ( import (
"context" "context"
"time" "time"
"github.com/micro/go-micro/v2/client"
) )
// Options contains configuration for the Store // Options contains configuration for the Store
@ -17,6 +19,8 @@ type Options struct {
Table string Table string
// Context should contain all implementation specific options, using context.WithValue. // Context should contain all implementation specific options, using context.WithValue.
Context context.Context Context context.Context
// Client to use for RPC
Client client.Client
} }
// Option sets values in Options // Option sets values in Options
@ -52,6 +56,13 @@ func WithContext(c context.Context) Option {
} }
} }
// WithClient sets the stores client to use for RPC
func WithClient(c client.Client) Option {
return func(o *Options) {
o.Client = c
}
}
// ReadOptions configures an individual Read operation // ReadOptions configures an individual Read operation
type ReadOptions struct { type ReadOptions struct {
Database, Table string Database, Table string

View File

@ -219,12 +219,16 @@ func NewStore(opts ...store.Option) store.Store {
o(&options) o(&options)
} }
if options.Client == nil {
options.Client = client.DefaultClient
}
service := &serviceStore{ service := &serviceStore{
options: options, options: options,
Database: options.Database, Database: options.Database,
Table: options.Table, Table: options.Table,
Nodes: options.Nodes, Nodes: options.Nodes,
Client: pb.NewStoreService("go.micro.store", client.DefaultClient), Client: pb.NewStoreService("go.micro.store", options.Client),
} }
return service return service