Skip to content

Commit

Permalink
find the good ip address using lookup
Browse files Browse the repository at this point in the history
Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>
  • Loading branch information
frouioui committed Dec 19, 2023
1 parent 830c39e commit b2de18e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"net"
"os"

"github.com/joho/godotenv"
Expand Down Expand Up @@ -69,9 +70,16 @@ func readConfig() (*config, error) {
// Get server address
serverAddress := os.Getenv("SERVER_ADDRESS")
if serverAddress == "" {
serverAddress, err = os.Hostname()
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, errors.Wrap(err, "could not resolve hostname")
panic(err)
}

for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
serverAddress = ipnet.IP.String()
break
}
}
}
c.address = serverAddress
Expand Down

0 comments on commit b2de18e

Please sign in to comment.