Registry init

This commit is contained in:
Asim 2016-03-15 22:20:21 +00:00
parent 64220dc0c5
commit f088074f29
6 changed files with 28 additions and 20 deletions

View File

@ -110,7 +110,7 @@ var (
"http": broker.NewBroker, "http": broker.NewBroker,
} }
DefaultRegistries = map[string]func([]string, ...registry.Option) registry.Registry{ DefaultRegistries = map[string]func(...registry.Option) registry.Registry{
"consul": registry.NewRegistry, "consul": registry.NewRegistry,
} }
@ -216,7 +216,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
} }
if r, ok := c.opts.Registries[name]; ok { if r, ok := c.opts.Registries[name]; ok {
n := r(strings.Split(ctx.String("registry_address"), ",")) n := r(registry.Addrs(strings.Split(ctx.String("registry_address"), ",")...))
*c.opts.Registry = n *c.opts.Registry = n
} else { } else {
return fmt.Errorf("Registry %s not found", name) return fmt.Errorf("Registry %s not found", name)

View File

@ -26,7 +26,7 @@ type Options struct {
Server *server.Server Server *server.Server
Brokers map[string]func(...broker.Option) broker.Broker Brokers map[string]func(...broker.Option) broker.Broker
Registries map[string]func([]string, ...registry.Option) registry.Registry Registries map[string]func(...registry.Option) registry.Registry
Selectors map[string]func(...selector.Option) selector.Selector Selectors map[string]func(...selector.Option) selector.Selector
Transports map[string]func([]string, ...transport.Option) transport.Transport Transports map[string]func([]string, ...transport.Option) transport.Transport
@ -100,7 +100,7 @@ func NewBroker(name string, b func(...broker.Option) broker.Broker) Option {
} }
// New registry func // New registry func
func NewRegistry(name string, r func([]string, ...registry.Option) registry.Registry) Option { func NewRegistry(name string, r func(...registry.Option) registry.Registry) Option {
return func(o *Options) { return func(o *Options) {
o.Registries[name] = r o.Registries[name] = r
} }

View File

@ -9,6 +9,6 @@ func init() {
cmd.DefaultRegistries["consul"] = NewRegistry cmd.DefaultRegistries["consul"] = NewRegistry
} }
func NewRegistry(addrs []string, opts ...registry.Option) registry.Registry { func NewRegistry(opts ...registry.Option) registry.Registry {
return registry.NewRegistry(addrs, opts...) return registry.NewRegistry(opts...)
} }

View File

@ -109,26 +109,26 @@ func decodeVersion(tags []string) (string, bool) {
return "", false return "", false
} }
func newConsulRegistry(addrs []string, opts ...Option) Registry { func newConsulRegistry(opts ...Option) Registry {
var opt Options var options Options
for _, o := range opts { for _, o := range opts {
o(&opt) o(&options)
} }
// use default config // use default config
config := consul.DefaultConfig() config := consul.DefaultConfig()
// set timeout // set timeout
if opt.Timeout > 0 { if options.Timeout > 0 {
config.HttpClient.Timeout = opt.Timeout config.HttpClient.Timeout = options.Timeout
} }
// check if there are any addrs // check if there are any addrs
if len(addrs) > 0 { if len(options.Addrs) > 0 {
addr, port, err := net.SplitHostPort(addrs[0]) addr, port, err := net.SplitHostPort(options.Addrs[0])
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
port = "8500" port = "8500"
addr = addrs[0] addr = options.Addrs[0]
config.Address = fmt.Sprintf("%s:%s", addr, port) config.Address = fmt.Sprintf("%s:%s", addr, port)
} else if err == nil { } else if err == nil {
config.Address = fmt.Sprintf("%s:%s", addr, port) config.Address = fmt.Sprintf("%s:%s", addr, port)
@ -136,10 +136,10 @@ func newConsulRegistry(addrs []string, opts ...Option) Registry {
} }
// requires secure connection? // requires secure connection?
if opt.Secure || opt.TLSConfig != nil { if options.Secure || options.TLSConfig != nil {
config.Scheme = "https" config.Scheme = "https"
// We're going to support InsecureSkipVerify // We're going to support InsecureSkipVerify
config.HttpClient.Transport = newTransport(opt.TLSConfig) config.HttpClient.Transport = newTransport(options.TLSConfig)
} }
// create the client // create the client
@ -148,7 +148,7 @@ func newConsulRegistry(addrs []string, opts ...Option) Registry {
cr := &consulRegistry{ cr := &consulRegistry{
Address: config.Address, Address: config.Address,
Client: client, Client: client,
Options: opt, Options: options,
} }
return cr return cr

View File

@ -8,6 +8,7 @@ import (
) )
type Options struct { type Options struct {
Addrs []string
Timeout time.Duration Timeout time.Duration
Secure bool Secure bool
TLSConfig *tls.Config TLSConfig *tls.Config
@ -24,6 +25,13 @@ type RegisterOptions struct {
Context context.Context Context context.Context
} }
// Addrs is the registry addresses to use
func Addrs(addrs ...string) Option {
return func(o *Options) {
o.Addrs = addrs
}
}
func Timeout(t time.Duration) Option { func Timeout(t time.Duration) Option {
return func(o *Options) { return func(o *Options) {
o.Timeout = t o.Timeout = t

View File

@ -21,13 +21,13 @@ type Option func(*Options)
type RegisterOption func(*RegisterOptions) type RegisterOption func(*RegisterOptions)
var ( var (
DefaultRegistry = newConsulRegistry([]string{}) DefaultRegistry = newConsulRegistry()
ErrNotFound = errors.New("not found") ErrNotFound = errors.New("not found")
) )
func NewRegistry(addrs []string, opt ...Option) Registry { func NewRegistry(opts ...Option) Registry {
return newConsulRegistry(addrs, opt...) return newConsulRegistry(opts...)
} }
// Register a service node. Additionally supply options such as TTL. // Register a service node. Additionally supply options such as TTL.