diff --git a/common.go b/common.go index a1138468..5a36e1c8 100644 --- a/common.go +++ b/common.go @@ -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 +} diff --git a/common_test.go b/common_test.go index 508aae83..b9a21fe1 100644 --- a/common_test.go +++ b/common_test.go @@ -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)) + } +} diff --git a/main.go b/main.go index fa670b73..231ff4cf 100644 --- a/main.go +++ b/main.go @@ -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 {