From 3d7187f405c355b24c6052eca19fe2db70cd682b Mon Sep 17 00:00:00 2001 From: chriss Date: Thu, 17 Nov 2016 21:03:54 +0000 Subject: [PATCH] Add the ability to pass consul client config via registry options. This can be used when you need to configure the consul client to use a specific configuration (E.G to pass a Token if consul is using ACL policies) --- registry/consul/options.go | 16 ++++++++++++++++ registry/consul_registry.go | 9 ++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 registry/consul/options.go diff --git a/registry/consul/options.go b/registry/consul/options.go new file mode 100644 index 00000000..3c28d45d --- /dev/null +++ b/registry/consul/options.go @@ -0,0 +1,16 @@ +package consul + +import ( + consul "github.com/hashicorp/consul/api" + "github.com/micro/go-micro/registry" + "golang.org/x/net/context" +) + +func Config(c *consul.Config) registry.Option { + return func(o *registry.Options) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, "consul_config", c) + } +} diff --git a/registry/consul_registry.go b/registry/consul_registry.go index 4eecde04..3d34c09d 100644 --- a/registry/consul_registry.go +++ b/registry/consul_registry.go @@ -53,6 +53,13 @@ func newConsulRegistry(opts ...Option) Registry { // use default config config := consul.DefaultConfig() + if options.Context != nil { + // Use the consul config passed in the options, if available + c := options.Context.Value("consul_config") + if c != nil { + config = c.(*consul.Config) + } + } // set timeout if options.Timeout > 0 { @@ -253,7 +260,7 @@ func (c *consulRegistry) ListServices() ([]*Service, error) { var services []*Service - for service, _ := range rsp { + for service := range rsp { services = append(services, &Service{Name: service}) }