diff --git a/internal/backend/backend.go b/internal/backend/backend.go index cdc10bc..07af96b 100644 --- a/internal/backend/backend.go +++ b/internal/backend/backend.go @@ -13,7 +13,7 @@ type Backends map[string][]*Backend type Backend struct { Name string - Url string + URL *url.URL Proxy *httputil.ReverseProxy Live bool Monitor []MonitorFrame @@ -51,12 +51,15 @@ func CreateBackends(nameUrlPairs string) (Backends, error) { k := split[i-1] _, ok := backends[k] backendUrl := split[i] - b, _ := createBackend(k, backendUrl) + b, err := createBackend(k, backendUrl) + if err != nil { + return nil, err + } if !ok { backends[k] = []*Backend{b} } else { for _, existingBackend := range backends[k] { - if existingBackend.Url == backendUrl { + if existingBackend.URL.Host == b.URL.Host { return nil, fmt.Errorf("url (%s) already exist in backend (%s)", backendUrl, k) } } @@ -74,12 +77,15 @@ func createBackend(key, urlString string) (*Backend, error) { if err != nil { return nil, err } + if urlParsed.Host == "" { + return nil, fmt.Errorf("empty host for url (%s) in backend (%s)", urlString, key) + } proxy := httputil.NewSingleHostReverseProxy(urlParsed) return &Backend{ string(strip([]byte(fmt.Sprintf("%s%s", key, urlString)))), - urlString, + urlParsed, proxy, false, []MonitorFrame{}, diff --git a/internal/backend/backend_test.go b/internal/backend/backend_test.go index 769566f..7e0389c 100644 --- a/internal/backend/backend_test.go +++ b/internal/backend/backend_test.go @@ -9,17 +9,19 @@ func TestCreateBackends(t *testing.T) { var tests = []struct { nameUrlPairs string key string - values []Backend + host string }{ - {"a,b", "a", []Backend{{Url: "b"}}}, + {"a,http://b:1234", "a", "b:1234"}, } for _, test := range tests { - result, _ := CreateBackends(test.nameUrlPairs) - for i, v := range result[test.key] { - if v.Url != test.values[i].Url { - t.Errorf("wrong backend url: want (%s) got (%s)\n", test.values[i].Url, v.Url) - } + be, err := CreateBackends(test.nameUrlPairs) + if err != nil { + t.Error(err) + } + b := be[test.key][0] + if b.URL.Host != test.host { + t.Errorf("wrong backend url: want (%s) got (%s)\n", test.host, b.URL.Host) } } } @@ -34,17 +36,17 @@ func TestCreateBackendsErrors(t *testing.T) { {"a,b,c", fmt.Errorf("backends must be a comma-separated list containing even number of items")}, {",", fmt.Errorf("nameUrlPair at index 0 must have a value")}, {"a,", fmt.Errorf("nameUrlPair at index 1 must have a value")}, - {"a,b,a,b", fmt.Errorf("url (b) already exist in backend (a)")}, - {"a,b", nil}, + {"a,b,a,b", fmt.Errorf("empty host for url (b) in backend (a)")}, + {"a,http://b:1234,a,http://b:1234", fmt.Errorf("url (http://b:1234) already exist in backend (a)")}, } for _, test := range tests { _, err := CreateBackends(test.nameUrlPairs) - if test.err != nil && err.Error() != test.err.Error() { - t.Errorf("Want: %s\nGot: %s\n", test.err.Error(), err.Error()) + if err == nil { + t.Errorf("was expecting an error\n") } - if test.err == nil && err != nil { - t.Errorf("Was not expecting an error\n") + if test.err != nil && err.Error() != test.err.Error() { + t.Errorf("was expecting an error: want (%s)\ngot: (%s)\n", test.err.Error(), err.Error()) } } } diff --git a/internal/backend/monitor.go b/internal/backend/monitor.go index d1157a3..39ebf89 100644 --- a/internal/backend/monitor.go +++ b/internal/backend/monitor.go @@ -25,7 +25,7 @@ func (b Backends) Monitor() chan interface{} { func (be *Backend) monitor(messages chan interface{}) { for { - latency := healthCheck(be.Url) + latency := healthCheck(be.URL.Host) be.Live = latency > 0 colorCode := getColorCode(latency) diff --git a/internal/backend/monitor_test.go b/internal/backend/monitor_test.go index 57deed5..c3daa6e 100644 --- a/internal/backend/monitor_test.go +++ b/internal/backend/monitor_test.go @@ -16,14 +16,14 @@ func TestHealthCheckBackend(t *testing.T) { } latency := healthCheck(url.Host) if latency == 0 { - t.Errorf("latency should be greated then 0, got (%v)", latency) + t.Errorf("wrong latency: want (>0) got (%v)\n", latency) } } func TestHealthCheckNoBackend(t *testing.T) { latency := healthCheck("notexists.local:1234") if latency > 0 { - t.Errorf("latency should be 0, got (%v)", latency) + t.Errorf("wrong latency: want (0) got (%v)\n", latency) } } diff --git a/internal/dashboard/templates/monitor.html b/internal/dashboard/templates/monitor.html index 731d36d..de12c58 100644 --- a/internal/dashboard/templates/monitor.html +++ b/internal/dashboard/templates/monitor.html @@ -14,7 +14,7 @@
{{range $value}}
-
{{.Url}}
+
{{.URL.Host}}
Latency: unavailable
{{range .Monitor}} diff --git a/internal/dashboard/templates/traffic.html b/internal/dashboard/templates/traffic.html index c658bee..e18c836 100644 --- a/internal/dashboard/templates/traffic.html +++ b/internal/dashboard/templates/traffic.html @@ -27,7 +27,7 @@ {{range $bvalue}}
  • -
    {{.Url}}
    +
    {{.URL.String}}
    Hits: {{.Hits}}
  • {{end}} diff --git a/readme.md b/readme.md index 833ce35..cd22d28 100644 --- a/readme.md +++ b/readme.md @@ -32,7 +32,9 @@ docker compose -f sample/compose.yaml up --build docker compose -f sample/compose.yaml down -for i in {1..10}; do curl localhost:8080; sleep 1; done; +for i in {1..10}; do curl -s localhost:8080 | grep h1; sleep 1; done; + +seq 1000 | parallel -n0 -j8 "curl -s http://localhost:8080 | grep h1" ``` ## Misc diff --git a/sample/compose.yaml b/sample/compose.yaml index 20eae27..f9ede7a 100644 --- a/sample/compose.yaml +++ b/sample/compose.yaml @@ -32,7 +32,7 @@ services: dockerfile: Dockerfile environment: - FE=localhost,sample - - BE=sample,http://be1,sample,http://be2,sample,http://be3 + - BE=sample,http://host.docker.internal:8081,sample,http://host.docker.internal:8082,sample,http://host.docker.internal:8083 ports: - "8080:8080" - "8000:8000" \ No newline at end of file