From 1b18730d5433e00e97f44370736506220a64aece Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Thu, 14 May 2020 11:25:19 +0100 Subject: [PATCH] Custom micro client --- auth/service/service.go | 1 - config/cmd/cmd.go | 46 +++++++++++++++++++++++------------------ service.go | 2 +- util/wrapper/wrapper.go | 6 ++---- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/auth/service/service.go b/auth/service/service.go index be316525..ecb605f4 100644 --- a/auth/service/service.go +++ b/auth/service/service.go @@ -300,7 +300,6 @@ func serializeAccount(a *pb.Account) *auth.Account { // NewAuth returns a new instance of the Auth service func NewAuth(opts ...auth.Option) auth.Auth { options := auth.NewOptions(opts...) - if options.Client == nil { options.Client = client.DefaultClient } diff --git a/config/cmd/cmd.go b/config/cmd/cmd.go index de805f6c..8886994c 100644 --- a/config/cmd/cmd.go +++ b/config/cmd/cmd.go @@ -11,6 +11,7 @@ import ( "github.com/micro/go-micro/v2/auth/provider" "github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/client" + "github.com/micro/go-micro/v2/client/grpc" "github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/config" configSrc "github.com/micro/go-micro/v2/config/source" @@ -26,6 +27,7 @@ import ( "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/transport" + "github.com/micro/go-micro/v2/util/wrapper" // clients cgrpc "github.com/micro/go-micro/v2/client/grpc" @@ -469,21 +471,9 @@ func (c *cmd) Before(ctx *cli.Context) error { var serverOpts []server.Option var clientOpts []client.Option - // Set the client. This must be first since we use the client below - if name := ctx.String("client"); len(name) > 0 { - // only change if we have the client and type differs - if cl, ok := c.opts.Clients[name]; ok && (*c.opts.Client).String() != name { - *c.opts.Client = cl() - } - } - - // Set the server - if name := ctx.String("server"); len(name) > 0 { - // only change if we have the server and type differs - if s, ok := c.opts.Servers[name]; ok && (*c.opts.Server).String() != name { - *c.opts.Server = s() - } - } + // setup a client to use when calling the runtime + authFn := func() auth.Auth { return *c.opts.Auth } + microClient := wrapper.AuthClient(authFn, grpc.NewClient()) // Set the store if name := ctx.String("store"); len(name) > 0 { @@ -492,7 +482,7 @@ func (c *cmd) Before(ctx *cli.Context) error { return fmt.Errorf("Unsupported store: %s", name) } - *c.opts.Store = s(store.WithClient(*c.opts.Client)) + *c.opts.Store = s(store.WithClient(microClient)) } // Set the runtime @@ -502,7 +492,7 @@ func (c *cmd) Before(ctx *cli.Context) error { return fmt.Errorf("Unsupported runtime: %s", name) } - *c.opts.Runtime = r(runtime.WithClient(*c.opts.Client)) + *c.opts.Runtime = r(runtime.WithClient(microClient)) } // Set the tracer @@ -521,7 +511,7 @@ func (c *cmd) Before(ctx *cli.Context) error { if !ok { return fmt.Errorf("Unsupported auth: %s", name) } - *c.opts.Auth = a(auth.WithClient(*c.opts.Client)) + *c.opts.Auth = a(auth.WithClient(microClient)) serverOpts = append(serverOpts, server.Auth(*c.opts.Auth)) } @@ -554,7 +544,7 @@ func (c *cmd) Before(ctx *cli.Context) error { return fmt.Errorf("Registry %s not found", name) } - *c.opts.Registry = r(registrySrv.WithClient(*c.opts.Client)) + *c.opts.Registry = r(registrySrv.WithClient(microClient)) serverOpts = append(serverOpts, server.Registry(*c.opts.Registry)) clientOpts = append(clientOpts, client.Registry(*c.opts.Registry)) @@ -594,6 +584,22 @@ func (c *cmd) Before(ctx *cli.Context) error { clientOpts = append(clientOpts, client.Transport(*c.opts.Transport)) } + // Set the client + if name := ctx.String("client"); len(name) > 0 { + // only change if we have the client and type differs + if cl, ok := c.opts.Clients[name]; ok && (*c.opts.Client).String() != name { + *c.opts.Client = cl() + } + } + + // Set the server + if name := ctx.String("server"); len(name) > 0 { + // only change if we have the server and type differs + if s, ok := c.opts.Servers[name]; ok && (*c.opts.Server).String() != name { + *c.opts.Server = s() + } + } + // Parse the server options metadata := make(map[string]string) for _, d := range ctx.StringSlice("server_metadata") { @@ -725,7 +731,7 @@ func (c *cmd) Before(ctx *cli.Context) error { (*c.opts.Auth).Init(authOpts...) if ctx.String("config") == "service" { - opt := config.WithSource(configSrv.NewSource(configSrc.WithClient(*c.opts.Client))) + opt := config.WithSource(configSrv.NewSource(configSrc.WithClient(microClient))) if err := (*c.opts.Config).Init(opt); err != nil { logger.Fatalf("Error configuring config: %v", err) } diff --git a/service.go b/service.go index ecbd6422..b3ebc1ae 100644 --- a/service.go +++ b/service.go @@ -43,7 +43,7 @@ func newService(opts ...Option) Service { // wrap client to inject From-Service header on any calls options.Client = wrapper.FromService(serviceName, options.Client) options.Client = wrapper.TraceCall(serviceName, trace.DefaultTracer, options.Client) - options.Client = wrapper.AuthClient(serviceName, options.Server.Options().Id, authFn, options.Client) + options.Client = wrapper.AuthClient(authFn, options.Client) // wrap the server to provide handler stats options.Server.Init( diff --git a/util/wrapper/wrapper.go b/util/wrapper/wrapper.go index 041feedd..51672f0a 100644 --- a/util/wrapper/wrapper.go +++ b/util/wrapper/wrapper.go @@ -132,8 +132,6 @@ func TraceHandler(t trace.Tracer) server.HandlerWrapper { type authWrapper struct { client.Client - name string - id string auth func() auth.Auth } @@ -170,8 +168,8 @@ func (a *authWrapper) Call(ctx context.Context, req client.Request, rsp interfac } // AuthClient wraps requests with the auth header -func AuthClient(name string, id string, auth func() auth.Auth, c client.Client) client.Client { - return &authWrapper{c, name, id, auth} +func AuthClient(auth func() auth.Auth, c client.Client) client.Client { + return &authWrapper{c, auth} } // AuthHandler wraps a server handler to perform auth