From b8815dff14e81ab2b17611eba916c12e38be33f5 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Thu, 3 Oct 2019 16:16:25 +0100 Subject: [PATCH] Append a port to address if it does not exist --- registry/consul/consul.go | 4 ++-- registry/etcd/etcd.go | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/registry/consul/consul.go b/registry/consul/consul.go index 007d7369..152aae69 100644 --- a/registry/consul/consul.go +++ b/registry/consul/consul.go @@ -107,9 +107,9 @@ func configure(c *consulRegistry, opts ...registry.Option) { if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { port = "8500" addr = address - addrs = append(addrs, fmt.Sprintf("%s:%s", addr, port)) + addrs = append(addrs, net.JoinHostPort(addr, port)) } else if err == nil { - addrs = append(addrs, fmt.Sprintf("%s:%s", addr, port)) + addrs = append(addrs, net.JoinHostPort(addr, port)) } } diff --git a/registry/etcd/etcd.go b/registry/etcd/etcd.go index c4419487..f2780058 100644 --- a/registry/etcd/etcd.go +++ b/registry/etcd/etcd.go @@ -6,6 +6,7 @@ import ( "crypto/tls" "encoding/json" "errors" + "net" "path" "strings" "sync" @@ -74,11 +75,18 @@ func configure(e *etcdRegistry, opts ...registry.Option) error { var cAddrs []string - for _, addr := range e.options.Addrs { - if len(addr) == 0 { + for _, address := range e.options.Addrs { + if len(address) == 0 { continue } - cAddrs = append(cAddrs, addr) + addr, port, err := net.SplitHostPort(address) + if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { + port = "2379" + addr = address + cAddrs = append(cAddrs, net.JoinHostPort(addr, port)) + } else if err == nil { + cAddrs = append(cAddrs, net.JoinHostPort(addr, port)) + } } // if we got addrs then we'll update