Custom micro client
This commit is contained in:
parent
5764519f5b
commit
1b18730d54
@ -300,7 +300,6 @@ func serializeAccount(a *pb.Account) *auth.Account {
|
|||||||
// NewAuth returns a new instance of the Auth service
|
// NewAuth returns a new instance of the Auth service
|
||||||
func NewAuth(opts ...auth.Option) auth.Auth {
|
func NewAuth(opts ...auth.Option) auth.Auth {
|
||||||
options := auth.NewOptions(opts...)
|
options := auth.NewOptions(opts...)
|
||||||
|
|
||||||
if options.Client == nil {
|
if options.Client == nil {
|
||||||
options.Client = client.DefaultClient
|
options.Client = client.DefaultClient
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/micro/go-micro/v2/auth/provider"
|
"github.com/micro/go-micro/v2/auth/provider"
|
||||||
"github.com/micro/go-micro/v2/broker"
|
"github.com/micro/go-micro/v2/broker"
|
||||||
"github.com/micro/go-micro/v2/client"
|
"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/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"
|
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/server"
|
||||||
"github.com/micro/go-micro/v2/store"
|
"github.com/micro/go-micro/v2/store"
|
||||||
"github.com/micro/go-micro/v2/transport"
|
"github.com/micro/go-micro/v2/transport"
|
||||||
|
"github.com/micro/go-micro/v2/util/wrapper"
|
||||||
|
|
||||||
// clients
|
// clients
|
||||||
cgrpc "github.com/micro/go-micro/v2/client/grpc"
|
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 serverOpts []server.Option
|
||||||
var clientOpts []client.Option
|
var clientOpts []client.Option
|
||||||
|
|
||||||
// Set the client. This must be first since we use the client below
|
// setup a client to use when calling the runtime
|
||||||
if name := ctx.String("client"); len(name) > 0 {
|
authFn := func() auth.Auth { return *c.opts.Auth }
|
||||||
// only change if we have the client and type differs
|
microClient := wrapper.AuthClient(authFn, grpc.NewClient())
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the store
|
// Set the store
|
||||||
if name := ctx.String("store"); len(name) > 0 {
|
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)
|
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
|
// Set the runtime
|
||||||
@ -502,7 +492,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
|
|||||||
return fmt.Errorf("Unsupported runtime: %s", name)
|
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
|
// Set the tracer
|
||||||
@ -521,7 +511,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Unsupported auth: %s", name)
|
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))
|
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)
|
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))
|
serverOpts = append(serverOpts, server.Registry(*c.opts.Registry))
|
||||||
clientOpts = append(clientOpts, client.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))
|
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
|
// Parse the server options
|
||||||
metadata := make(map[string]string)
|
metadata := make(map[string]string)
|
||||||
for _, d := range ctx.StringSlice("server_metadata") {
|
for _, d := range ctx.StringSlice("server_metadata") {
|
||||||
@ -725,7 +731,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(configSrc.WithClient(*c.opts.Client)))
|
opt := config.WithSource(configSrv.NewSource(configSrc.WithClient(microClient)))
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func newService(opts ...Option) Service {
|
|||||||
// wrap client to inject From-Service header on any calls
|
// wrap client to inject From-Service header on any calls
|
||||||
options.Client = wrapper.FromService(serviceName, options.Client)
|
options.Client = wrapper.FromService(serviceName, options.Client)
|
||||||
options.Client = wrapper.TraceCall(serviceName, trace.DefaultTracer, 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
|
// wrap the server to provide handler stats
|
||||||
options.Server.Init(
|
options.Server.Init(
|
||||||
|
@ -132,8 +132,6 @@ func TraceHandler(t trace.Tracer) server.HandlerWrapper {
|
|||||||
|
|
||||||
type authWrapper struct {
|
type authWrapper struct {
|
||||||
client.Client
|
client.Client
|
||||||
name string
|
|
||||||
id string
|
|
||||||
auth func() auth.Auth
|
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
|
// AuthClient wraps requests with the auth header
|
||||||
func AuthClient(name string, id string, auth func() auth.Auth, c client.Client) client.Client {
|
func AuthClient(auth func() auth.Auth, c client.Client) client.Client {
|
||||||
return &authWrapper{c, name, id, auth}
|
return &authWrapper{c, auth}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthHandler wraps a server handler to perform auth
|
// AuthHandler wraps a server handler to perform auth
|
||||||
|
Loading…
Reference in New Issue
Block a user