Skip to content

Commit

Permalink
support "--serve=[::]:8000" for ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 23, 2021
1 parent 8c8f947 commit e23757c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/api/serve_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,24 +478,32 @@ func serveImpl(serveOptions ServeOptions, buildOptions BuildOptions) (ServeResul
}
}

// Pick the port
// Determine the host
var listener net.Listener
network := "tcp4"
host := "127.0.0.1"
if serveOptions.Host != "" {
host = serveOptions.Host

// Only use "tcp4" if this is an IPv4 address, otherwise use "tcp"
if ip := net.ParseIP(host); ip == nil || ip.To4() == nil {
network = "tcp"
}
}

// Pick the port
if serveOptions.Port == 0 {
// Default to picking a "800X" port
for port := 8000; port <= 8009; port++ {
if result, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)); err == nil {
if result, err := net.Listen(network, net.JoinHostPort(host, fmt.Sprintf("%d", port))); err == nil {
listener = result
break
}
}
}
if listener == nil {
// Otherwise pick the provided port
if result, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, serveOptions.Port)); err != nil {
if result, err := net.Listen(network, net.JoinHostPort(host, fmt.Sprintf("%d", serveOptions.Port))); err != nil {
return ServeResult{}, err
} else {
listener = result
Expand Down

0 comments on commit e23757c

Please sign in to comment.