diff --git a/api/resolver/vpath/vpath.go b/api/resolver/vpath/vpath.go index cf407ded..f653a86f 100644 --- a/api/resolver/vpath/vpath.go +++ b/api/resolver/vpath/vpath.go @@ -3,7 +3,6 @@ package vpath import ( "errors" - "fmt" "net/http" "regexp" "strings" @@ -28,8 +27,6 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) { return nil, errors.New("unknown name") } - fmt.Println(req.URL.Path) - parts := strings.Split(req.URL.Path[1:], "/") if len(parts) == 1 { return &resolver.Endpoint{ diff --git a/auth/options.go b/auth/options.go index cb395d04..da0982b7 100644 --- a/auth/options.go +++ b/auth/options.go @@ -4,6 +4,7 @@ import ( "time" "github.com/micro/go-micro/v2/auth/provider" + "github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/store" ) @@ -12,10 +13,12 @@ func NewOptions(opts ...Option) Options { for _, o := range opts { o(&options) } - if len(options.Namespace) == 0 { options.Namespace = DefaultNamespace } + if options.Client == nil { + options.Client = client.DefaultClient + } return options } @@ -39,6 +42,8 @@ type Options struct { LoginURL string // Store to back auth Store store.Store + // Client to use for RPC + Client client.Client } 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 { // Metadata associated with the account Metadata map[string]string diff --git a/auth/service/service.go b/auth/service/service.go index c0f7e7dd..fbec81d0 100644 --- a/auth/service/service.go +++ b/auth/service/service.go @@ -42,9 +42,11 @@ func (s *svc) Init(opts ...auth.Option) { o(&s.options) } - dc := client.DefaultClient - s.auth = pb.NewAuthService("go.micro.auth", dc) - s.rule = pb.NewRulesService("go.micro.auth", dc) + if s.options.Client == nil { + s.options.Client = client.DefaultClient + } + 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, // we can decode tokens with the type "JWT" locally diff --git a/client/options.go b/client/options.go index fed5992f..5c8f833d 100644 --- a/client/options.go +++ b/client/options.go @@ -4,7 +4,6 @@ import ( "context" "time" - "github.com/micro/go-micro/v2/auth" "github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/codec" @@ -17,7 +16,6 @@ type Options struct { ContentType string // Plugged interfaces - Auth auth.Auth Broker broker.Broker Codecs map[string]codec.NewCodec Registry registry.Registry @@ -105,7 +103,6 @@ func NewOptions(options ...Option) Options { }, PoolSize: DefaultPoolSize, PoolTTL: DefaultPoolTTL, - Auth: auth.DefaultAuth, Broker: broker.DefaultBroker, Selector: selector.DefaultSelector, 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 func Codec(contentType string, c codec.NewCodec) Option { return func(o *Options) { diff --git a/config/cmd/cmd.go b/config/cmd/cmd.go index 7cec8f99..c2476a7e 100644 --- a/config/cmd/cmd.go +++ b/config/cmd/cmd.go @@ -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) } diff --git a/config/cmd/options.go b/config/cmd/options.go index 7ec9e2b4..1dfc8076 100644 --- a/config/cmd/options.go +++ b/config/cmd/options.go @@ -100,6 +100,12 @@ func Registry(r *registry.Registry) Option { } } +func Runtime(r *runtime.Runtime) Option { + return func(o *Options) { + o.Runtime = r + } +} + func Transport(t *transport.Transport) Option { return func(o *Options) { o.Transport = t diff --git a/config/source/options.go b/config/source/options.go index d4a2ba09..cd694a77 100644 --- a/config/source/options.go +++ b/config/source/options.go @@ -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 + } +} diff --git a/config/source/service/service.go b/config/source/service/service.go index cc5499eb..9bacdf63 100644 --- a/config/source/service/service.go +++ b/config/source/service/service.go @@ -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, diff --git a/options.go b/options.go index d216b53e..e807e496 100644 --- a/options.go +++ b/options.go @@ -14,6 +14,7 @@ import ( "github.com/micro/go-micro/v2/debug/profile" "github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/registry" + "github.com/micro/go-micro/v2/runtime" "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/transport" @@ -29,6 +30,7 @@ type Options struct { Server server.Server Store store.Store Registry registry.Registry + Runtime runtime.Runtime Transport transport.Transport Profile profile.Profile @@ -55,6 +57,7 @@ func newOptions(opts ...Option) Options { Server: server.DefaultServer, Store: store.DefaultStore, Registry: registry.DefaultRegistry, + Runtime: runtime.DefaultRuntime, Transport: transport.DefaultTransport, Context: context.Background(), Signal: true, @@ -152,7 +155,6 @@ func Tracer(t trace.Tracer) Option { func Auth(a auth.Auth) Option { return func(o *Options) { o.Auth = a - o.Client.Init(client.Auth(a)) o.Server.Init(server.Auth(a)) } } @@ -182,6 +184,13 @@ func Transport(t transport.Transport) Option { } } +// Runtime sets the runtime +func Runtime(r runtime.Runtime) Option { + return func(o *Options) { + o.Runtime = r + } +} + // Convenience options // Address sets the address of the server diff --git a/runtime/options.go b/runtime/options.go index d7d9fe90..dea936b9 100644 --- a/runtime/options.go +++ b/runtime/options.go @@ -3,6 +3,8 @@ package runtime import ( "context" "io" + + "github.com/micro/go-micro/v2/client" ) type Option func(o *Options) @@ -17,6 +19,8 @@ type Options struct { Source string // Base image to use Image string + // Client to use when making requests + Client client.Client } // WithSource sets the base image / repository @@ -47,6 +51,13 @@ func WithImage(t string) Option { } } +// WithClient sets the client to use +func WithClient(c client.Client) Option { + return func(o *Options) { + o.Client = c + } +} + type CreateOption func(o *CreateOptions) type ReadOption func(o *ReadOptions) diff --git a/runtime/service/service.go b/runtime/service/service.go index e6bc03fc..90451028 100644 --- a/runtime/service/service.go +++ b/runtime/service/service.go @@ -24,6 +24,9 @@ func (s *svc) Init(opts ...runtime.Option) error { o(&s.options) } + // reset the runtime as the client could have changed + s.runtime = pb.NewRuntimeService(runtime.DefaultName, s.options.Client) + return nil } @@ -278,19 +281,17 @@ func (s *svc) String() string { // NewRuntime creates new service runtime and returns it func NewRuntime(opts ...runtime.Option) runtime.Runtime { - // get default options - options := runtime.Options{} + var options runtime.Options - // apply requested options for _, o := range opts { o(&options) } - - // create default client - cli := client.DefaultClient + if options.Client == nil { + options.Client = client.DefaultClient + } return &svc{ options: options, - runtime: pb.NewRuntimeService(runtime.DefaultName, cli), + runtime: pb.NewRuntimeService(runtime.DefaultName, options.Client), } } diff --git a/service.go b/service.go index c81127d2..f3ba2086 100644 --- a/service.go +++ b/service.go @@ -3,7 +3,7 @@ package micro import ( "os" "os/signal" - "runtime" + rtime "runtime" "strings" "sync" @@ -15,6 +15,7 @@ import ( "github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/plugin" + "github.com/micro/go-micro/v2/runtime" "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" signalutil "github.com/micro/go-micro/v2/util/signal" @@ -98,6 +99,7 @@ func (s *service) Init(opts ...Option) { cmd.Auth(&s.opts.Auth), cmd.Broker(&s.opts.Broker), cmd.Registry(&s.opts.Registry), + cmd.Runtime(&s.opts.Runtime), cmd.Transport(&s.opts.Transport), cmd.Client(&s.opts.Client), cmd.Config(&s.opts.Config), @@ -112,13 +114,10 @@ func (s *service) Init(opts ...Option) { name := s.opts.Cmd.App().Name s.opts.Store.Init(store.Table(name)) - // TODO: replace Cmd.Init with config.Load - // Right now we're just going to load a token - // May need to re-read value on change - // TODO: should be scoped to micro/auth/token - // if tk, _ := config.Get("token"); len(tk) > 0 { - // s.opts.Auth.Init(auth.ServiceToken(tk)) - // } + // 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.Store.Init(store.WithClient(s.Client())) }) } @@ -192,9 +191,9 @@ func (s *service) Run() error { // start the profiler if s.opts.Profile != nil { // to view mutex contention - runtime.SetMutexProfileFraction(5) + rtime.SetMutexProfileFraction(5) // to view blocking profile - runtime.SetBlockProfileRate(1) + rtime.SetBlockProfileRate(1) if err := s.opts.Profile.Start(); err != nil { return err diff --git a/store/options.go b/store/options.go index 27224731..d89cb766 100644 --- a/store/options.go +++ b/store/options.go @@ -3,6 +3,8 @@ package store import ( "context" "time" + + "github.com/micro/go-micro/v2/client" ) // Options contains configuration for the Store @@ -17,6 +19,8 @@ type Options struct { Table string // Context should contain all implementation specific options, using context.WithValue. Context context.Context + // Client to use for RPC + Client client.Client } // 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 type ReadOptions struct { Database, Table string diff --git a/store/service/service.go b/store/service/service.go index fbf04388..60adbb9e 100644 --- a/store/service/service.go +++ b/store/service/service.go @@ -219,12 +219,16 @@ func NewStore(opts ...store.Option) store.Store { o(&options) } + if options.Client == nil { + options.Client = client.DefaultClient + } + service := &serviceStore{ options: options, Database: options.Database, Table: options.Table, Nodes: options.Nodes, - Client: pb.NewStoreService("go.micro.store", client.DefaultClient), + Client: pb.NewStoreService("go.micro.store", options.Client), } return service