Show URLs on starting

This commit is contained in:
Iwasaki Yudai 2015-08-22 13:10:08 +09:00
parent 8923220528
commit b18d35f267

View File

@ -13,6 +13,7 @@ import (
"github.com/elazarl/go-bindata-assetfs" "github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/kr/pty" "github.com/kr/pty"
"net"
) )
type App struct { type App struct {
@ -68,7 +69,17 @@ func (app *App) Run() error {
siteHandler = wrapLogger(siteHandler) siteHandler = wrapLogger(siteHandler)
log.Printf("Server is running at %s, command: %s", endpoint+path, strings.Join(app.options.Command, " ")) log.Printf(
"Server is starting with command: %s",
strings.Join(app.options.Command, " "),
)
if app.options.Address != "" {
log.Printf("URL: %s", "http://"+endpoint+path)
} else {
for _, address := range listAddresses() {
log.Printf("URL: %s", "http://"+address+":"+app.options.Port+path)
}
}
if err := http.ListenAndServe(endpoint, siteHandler); err != nil { if err := http.ListenAndServe(endpoint, siteHandler); err != nil {
return err return err
} }
@ -153,3 +164,24 @@ func generateRandomString(length int) string {
} }
return string(n) return string(n)
} }
func listAddresses() (addresses []string) {
ifaces, _ := net.Interfaces()
addresses = make([]string, 0, len(ifaces))
for _, iface := range ifaces {
ifAddrs, _ := iface.Addrs()
for _, ifAddr := range ifAddrs {
switch v := ifAddr.(type) {
case *net.IPNet:
addresses = append(addresses, v.IP.String())
case *net.IPAddr:
addresses = append(addresses, v.IP.To16().String())
addresses = append(addresses, v.IP.To4().String())
}
}
}
return
}