Skip to content

Commit

Permalink
print the listening URLs after Ran starts. For example: Listening on h…
Browse files Browse the repository at this point in the history
  • Loading branch information
m3ng9i committed Aug 8, 2016
1 parent 408e527 commit 888f078
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions ran.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import "syscall"
import "os/signal"
import "net"
import "net/http"
import "os"
import "fmt"
Expand All @@ -24,6 +25,66 @@ func catchSignal() {
}



// Get all available IPv4 addresses in system's network interface.
func getIPAutomaticly() (ip []string, e error) {
iface, err := net.Interfaces()
if err != nil {
e = err
return
}

for _, i := range iface {
addrs, err := i.Addrs()
if e != nil {
e = err
return
}
for _, a := range addrs {
add := net.ParseIP(strings.SplitN(a.String(), "/", 2)[0])
if add.To4() != nil {
ip = append(ip, add.String())
}
}
}

for _, i := range ip {
if i == "127.0.0.1" {
return
}
}

// add loopback
ip = append(ip, "127.0.0.1")

return
}


// Get all Listening address, like: http://127.0.0.1:8080
func getListeningAddr() (addr []string, err error) {
ips, err := getIPAutomaticly()
if err != nil {
return
}

for _, i := range ips {
if global.Config.TLS != nil {
if global.Config.TLS.Policy == global.TLSOnly {
addr = append(addr, fmt.Sprintf("https://%s:%d", i, global.Config.TLS.Port))
} else {
addr = append(addr, fmt.Sprintf("http://%s:%d", i, global.Config.Port))
addr = append(addr, fmt.Sprintf("https://%s:%d", i, global.Config.TLS.Port))
}
} else {
addr = append(addr, fmt.Sprintf("http://%s:%d", i, global.Config.Port))
}
}

return
}


func startLog() {
msg := "System: Ran is running on "

Expand All @@ -48,6 +109,15 @@ func startLog() {
}

global.Logger.Info(msg)

addr, err := getListeningAddr()
if err != nil {
global.Logger.Error(err)
} else {
for _, i := range addr {
global.Logger.Infof("System: Listening on %s", i)
}
}
}


Expand Down

0 comments on commit 888f078

Please sign in to comment.