proxy: pass addrs and scheme into newDirector
parent
99e9f561ee
commit
1ea3197feb
2
main.go
2
main.go
|
@ -217,7 +217,7 @@ func startProxy() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ph, err := proxy.NewHandler(pt, (*peers).Endpoints())
|
||||
ph, err := proxy.NewHandler(pt, (*peers).Addrs())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package proxy
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
@ -15,27 +14,15 @@ const (
|
|||
endpointFailureWait = 5 * time.Second
|
||||
)
|
||||
|
||||
func newDirector(urls []string) (*director, error) {
|
||||
if len(urls) == 0 {
|
||||
return nil, errors.New("one or more endpoints required")
|
||||
func newDirector(scheme string, addrs []string) (*director, error) {
|
||||
if len(addrs) == 0 {
|
||||
return nil, errors.New("one or more upstream addresses required")
|
||||
}
|
||||
|
||||
endpoints := make([]*endpoint, len(urls))
|
||||
for i, v := range urls {
|
||||
u, err := url.Parse(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid endpoint %q: %v", v, err)
|
||||
}
|
||||
|
||||
if u.Scheme == "" {
|
||||
return nil, fmt.Errorf("invalid endpoint %q: scheme required", v)
|
||||
}
|
||||
|
||||
if u.Host == "" {
|
||||
return nil, fmt.Errorf("invalid endpoint %q: host empty", v)
|
||||
}
|
||||
|
||||
endpoints[i] = newEndpoint(*u)
|
||||
endpoints := make([]*endpoint, len(addrs))
|
||||
for i, addr := range addrs {
|
||||
u := url.URL{Scheme: scheme, Host: addr}
|
||||
endpoints[i] = newEndpoint(u)
|
||||
}
|
||||
|
||||
d := director{ep: endpoints}
|
||||
|
|
|
@ -6,29 +6,49 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestNewDirectorEndpointValidation(t *testing.T) {
|
||||
func TestNewDirectorScheme(t *testing.T) {
|
||||
tests := []struct {
|
||||
good bool
|
||||
endpoints []string
|
||||
scheme string
|
||||
addrs []string
|
||||
want []string
|
||||
}{
|
||||
{true, []string{"http://192.0.2.8"}},
|
||||
{true, []string{"http://192.0.2.8:8001"}},
|
||||
{true, []string{"http://example.com"}},
|
||||
{true, []string{"http://example.com:8001"}},
|
||||
{true, []string{"http://192.0.2.8:8001", "http://example.com:8002"}},
|
||||
{
|
||||
scheme: "http",
|
||||
addrs: []string{"192.0.2.8:4002", "example.com:8080"},
|
||||
want: []string{"http://192.0.2.8:4002", "http://example.com:8080"},
|
||||
},
|
||||
{
|
||||
scheme: "https",
|
||||
addrs: []string{"192.0.2.8:4002", "example.com:8080"},
|
||||
want: []string{"https://192.0.2.8:4002", "https://example.com:8080"},
|
||||
},
|
||||
|
||||
{false, []string{"://"}},
|
||||
{false, []string{"http://"}},
|
||||
{false, []string{"192.0.2.8"}},
|
||||
{false, []string{"192.0.2.8:8001"}},
|
||||
{false, []string{""}},
|
||||
{false, []string{}},
|
||||
// accept addrs without a port
|
||||
{
|
||||
scheme: "http",
|
||||
addrs: []string{"192.0.2.8"},
|
||||
want: []string{"http://192.0.2.8"},
|
||||
},
|
||||
|
||||
// accept addrs even if they are garbage
|
||||
{
|
||||
scheme: "http",
|
||||
addrs: []string{"."},
|
||||
want: []string{"http://."},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
_, err := newDirector(tt.endpoints)
|
||||
if tt.good != (err == nil) {
|
||||
t.Errorf("#%d: expected success = %t, got err = %v", i, tt.good, err)
|
||||
got, err := newDirector(tt.scheme, tt.addrs)
|
||||
if err != nil {
|
||||
t.Errorf("#%d: newDirectory returned unexpected error: %v", i, err)
|
||||
}
|
||||
|
||||
for ii, wep := range tt.want {
|
||||
gep := got.ep[ii].URL.String()
|
||||
if !reflect.DeepEqual(wep, gep) {
|
||||
t.Errorf("#%d: want endpoints[%d] = %#v, got = %#v", i, ii, wep, gep)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewHandler(t *http.Transport, endpoints []string) (http.Handler, error) {
|
||||
d, err := newDirector(endpoints)
|
||||
func NewHandler(t *http.Transport, addrs []string) (http.Handler, error) {
|
||||
d, err := newDirector("http", addrs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue