Add consul-specific option for config (as registry)
This commit is contained in:
parent
68764ebafc
commit
8e4fd16aff
@ -74,6 +74,11 @@ func NewSource(opts ...source.Option) source.Source {
|
|||||||
// use default config
|
// use default config
|
||||||
config := api.DefaultConfig()
|
config := api.DefaultConfig()
|
||||||
|
|
||||||
|
// use the consul config passed in the options if any
|
||||||
|
if co, ok := options.Context.Value(consulConfigKey{}).(*api.Config); ok {
|
||||||
|
config = co
|
||||||
|
}
|
||||||
|
|
||||||
// check if there are any addrs
|
// check if there are any addrs
|
||||||
a, ok := options.Context.Value(addressKey{}).(string)
|
a, ok := options.Context.Value(addressKey{}).(string)
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -3,6 +3,7 @@ package consul
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/api"
|
||||||
"github.com/micro/go-micro/config/source"
|
"github.com/micro/go-micro/config/source"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,6 +12,7 @@ type prefixKey struct{}
|
|||||||
type stripPrefixKey struct{}
|
type stripPrefixKey struct{}
|
||||||
type dcKey struct{}
|
type dcKey struct{}
|
||||||
type tokenKey struct{}
|
type tokenKey struct{}
|
||||||
|
type consulConfigKey struct{}
|
||||||
|
|
||||||
// WithAddress sets the consul address
|
// WithAddress sets the consul address
|
||||||
func WithAddress(a string) source.Option {
|
func WithAddress(a string) source.Option {
|
||||||
@ -61,3 +63,13 @@ func WithToken(p string) source.Option {
|
|||||||
o.Context = context.WithValue(o.Context, tokenKey{}, p)
|
o.Context = context.WithValue(o.Context, tokenKey{}, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithConsulConfig set consul-specific options
|
||||||
|
func WithConsulConfig(c *api.Config) source.Option {
|
||||||
|
return func(o *source.Options) {
|
||||||
|
if o.Context == nil {
|
||||||
|
o.Context = context.Background()
|
||||||
|
}
|
||||||
|
o.Context = context.WithValue(o.Context, consulConfigKey{}, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user