web: fix ipv6 address issue (#1308)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-03-06 18:44:56 +03:00 committed by GitHub
parent ae60bea8d8
commit a864f812f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@ -48,35 +47,35 @@ func newService(opts ...Option) Service {
} }
func (s *service) genSrv() *registry.Service { func (s *service) genSrv() *registry.Service {
var host string
var port string
var err error
// default host:port // default host:port
parts := strings.Split(s.opts.Address, ":") if len(s.opts.Address) > 0 {
host := strings.Join(parts[:len(parts)-1], ":") host, port, err = net.SplitHostPort(s.opts.Address)
port, _ := strconv.Atoi(parts[len(parts)-1]) if err != nil {
log.Fatal(err)
}
}
// check the advertise address first // check the advertise address first
// if it exists then use it, otherwise // if it exists then use it, otherwise
// use the address // use the address
if len(s.opts.Advertise) > 0 { if len(s.opts.Advertise) > 0 {
parts = strings.Split(s.opts.Advertise, ":") host, port, err = net.SplitHostPort(s.opts.Address)
if err != nil {
// we have host:port log.Fatal(err)
if len(parts) > 1 {
// set the host
host = strings.Join(parts[:len(parts)-1], ":")
// get the port
if aport, _ := strconv.Atoi(parts[len(parts)-1]); aport > 0 {
port = aport
}
} else {
host = parts[0]
} }
} }
addr, err := maddr.Extract(host) addr, err := maddr.Extract(host)
if err != nil { if err != nil {
// best effort localhost log.Fatal(err)
addr = "127.0.0.1" }
if strings.Count(addr, ":") > 0 {
addr = "[" + addr + "]"
} }
return &registry.Service{ return &registry.Service{
@ -84,7 +83,7 @@ func (s *service) genSrv() *registry.Service {
Version: s.opts.Version, Version: s.opts.Version,
Nodes: []*registry.Node{{ Nodes: []*registry.Node{{
Id: s.opts.Id, Id: s.opts.Id,
Address: fmt.Sprintf("%s:%d", addr, port), Address: fmt.Sprintf("%s:%s", addr, port),
Metadata: s.opts.Metadata, Metadata: s.opts.Metadata,
}}, }},
} }