Display verbose listen address for HTTP server.

This commit is contained in:
Adrian Gallagher
2016-07-27 12:16:11 +10:00
parent ad056cd7dd
commit 70ede482bd
3 changed files with 44 additions and 2 deletions

View File

@@ -17,6 +17,7 @@ import (
"math"
"net/http"
"net/url"
"strconv"
"strings"
)
@@ -274,3 +275,16 @@ func EncodeURLValues(url string, values url.Values) string {
}
return path
}
func ExtractHost(address string) string {
host := SplitStrings(address, ":")[0]
if host == "" {
return "localhost"
}
return host
}
func ExtractPort(host string) int {
portStr := SplitStrings(host, ":")[1]
port, _ := strconv.Atoi(portStr)
return port
}

View File

@@ -206,3 +206,30 @@ func TestCalculateNetProfit(t *testing.T) {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
}
}
func TestExtractHost(t *testing.T) {
t.Parallel()
address := "localhost:1337"
expectedOutput := "localhost"
actualResult := ExtractHost(address)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
}
address = "192.168.1.100:1337"
expectedOutput = "192.168.1.100"
actualResult = ExtractHost(address)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
}
}
func TestExtractPort(t *testing.T) {
t.Parallel()
address := "localhost:1337"
expectedOutput := 1337
actualResult := ExtractPort(address)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
}
}

View File

@@ -138,9 +138,10 @@ func main() {
log.Println(err) // non fatal event
//bot.config.Webserver.Enabled = false
} else {
log.Println("HTTP Webserver support enabled.")
listenAddr := bot.config.Webserver.ListenAddress
log.Printf("HTTP Webserver support enabled. Listen URL: http://%s:%d/\n", ExtractHost(listenAddr), ExtractPort(listenAddr))
router := NewRouter(bot.exchanges)
log.Fatal(http.ListenAndServe(bot.config.Webserver.ListenAddress, router))
log.Fatal(http.ListenAndServe(listenAddr, router))
}
}
if !bot.config.Webserver.Enabled {