Skip to content

Commit

Permalink
Add optional Ports list
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed Feb 7, 2019
1 parent 113a5af commit b2ee419
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions xio/network/xhttp/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit b2ee419

Please sign in to comment.