Skip to content

Commit

Permalink
Custom micro client
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-toogood committed May 14, 2020
1 parent 5764519 commit 1b18730
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
1 change: 0 additions & 1 deletion auth/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
46 changes: 26 additions & 20 deletions config/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))
}

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions util/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ func TraceHandler(t trace.Tracer) server.HandlerWrapper {

type authWrapper struct {
client.Client
name string
id string
auth func() auth.Auth
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1b18730

Please sign in to comment.