Last active
January 23, 2020 22:04
-
-
Save Oppodelldog/9f2ba92f73e0225004006256e9d74fb6 to your computer and use it in GitHub Desktop.
When staring a server on all interfaces, log hints of how to access it easily. Nice to have in development of the app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func logAccessInfo(addr string, port int, path, protocol string) { | |
ifaces, err := net.Interfaces() | |
if err != nil { | |
logrus.Infof("access it via %s://%s%s", protocol, addr, path) | |
} | |
for _, i := range ifaces { | |
addrs, err := i.Addrs() | |
if err != nil { | |
continue | |
} | |
encodeV6 := func(ip interface{}) string { | |
var ipString string | |
switch v := ip.(type) { | |
case *net.IPAddr: | |
ipString = v.IP.String() | |
case *net.IPNet: | |
ipString = v.IP.String() | |
} | |
if strings.Contains(ipString, ":") { | |
ipString = "[" + ipString + "]" | |
} | |
return ipString | |
} | |
for _, a := range addrs { | |
address := encodeV6(a) | |
if strings.Index(address, "[::") == 0 { | |
continue | |
} | |
logrus.Infof("access it via %s://%s:%v%s", protocol, address, port, path) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment