import (
"fmt"
+ "net"
"net/http"
"os"
"strconv"
BaseUrl = "http://" + BaseUrl
}
+ l, err := net.Listen("tcp", ":"+strconv.Itoa(serverPort))
+ if err == nil {
+ l.Close()
+ } else {
+ jww.ERROR.Println("port", serverPort, "already in use, attempting to use an available port")
+ sp, err := helpers.FindAvailablePort()
+ if err != nil {
+ jww.ERROR.Println("Unable to find alternative port to use")
+ jww.ERROR.Fatalln(err)
+ }
+ serverPort = sp.Port
+ }
+
if serverAppend {
viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/")+":"+strconv.Itoa(serverPort))
} else {
import (
"bytes"
+ "fmt"
+ "net"
"strings"
)
}
return output
}
+
+func FindAvailablePort() (*net.TCPAddr, error) {
+ l, err := net.Listen("tcp", ":0")
+ if err == nil {
+ defer l.Close()
+ addr := l.Addr()
+ if a, ok := addr.(*net.TCPAddr); ok {
+ return a, nil
+ }
+ return nil, fmt.Errorf("Unable to obtain a valid tcp port. %v", addr)
+ }
+ return nil, err
+}