From b18d35f26780aa71b2d16136fce081fd953822e8 Mon Sep 17 00:00:00 2001 From: Iwasaki Yudai Date: Sat, 22 Aug 2015 13:10:08 +0900 Subject: [PATCH] Show URLs on starting --- app/app.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/app/app.go b/app/app.go index 7c278eb..5572987 100644 --- a/app/app.go +++ b/app/app.go @@ -13,6 +13,7 @@ import ( "github.com/elazarl/go-bindata-assetfs" "github.com/gorilla/websocket" "github.com/kr/pty" + "net" ) type App struct { @@ -68,7 +69,17 @@ func (app *App) Run() error { 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 { return err } @@ -153,3 +164,24 @@ func generateRandomString(length int) string { } 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 +}