From b2ee419446ba8056a2ceca55f8669d215962754a Mon Sep 17 00:00:00 2001 From: Richard Wilkes Date: Thu, 7 Feb 2019 12:04:36 -0800 Subject: [PATCH] Add optional Ports list --- xio/network/xhttp/web/server.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/xio/network/xhttp/web/server.go b/xio/network/xhttp/web/server.go index 91cb661..a60af86 100644 --- a/xio/network/xhttp/web/server.go +++ b/xio/network/xhttp/web/server.go @@ -31,6 +31,7 @@ type Server struct { ShutdownGracePeriod time.Duration Logger logadapter.Logger WebServer *http.Server + Ports []int ShutdownCallback func() StartedChan chan interface{} // If not nil, will be closed once the server is ready to accept connections addresses []string @@ -107,12 +108,21 @@ func (s *Server) Run() error { }() handler.ServeHTTP(sw, req) }) - address := s.WebServer.Addr - host, _, err := net.SplitHostPort(address) - if err != nil { - address = address + ":0" + var ln net.Listener + host, _, err := net.SplitHostPort(s.WebServer.Addr) + if err == nil { + ln, err = net.Listen("tcp", s.WebServer.Addr) + } else { + ports := s.Ports + if len(ports) == 0 { + ports = []int{0} + } + for _, one := range ports { + if ln, err = net.Listen("tcp", net.JoinHostPort(s.WebServer.Addr, strconv.Itoa(one))); err == nil { + break + } + } } - ln, err := net.Listen("tcp", address) if err != nil { return errs.Wrap(err) }