Skip to content

Commit

Permalink
Display verbose listen address for HTTP server.
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Jul 27, 2016
1 parent ad056cd commit 70ede48
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
14 changes: 14 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"math"
"net/http"
"net/url"
"strconv"
"strings"
)

Expand Down Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 70ede48

Please sign in to comment.