diff --git a/app/discovery/discovery.go b/app/discovery/discovery.go index 10af668..264143d 100644 --- a/app/discovery/discovery.go +++ b/app/discovery/discovery.go @@ -94,6 +94,19 @@ func (s *Service) Match(srv, src string) (string, bool) { return src, false } +// Servers return list of all servers, skips "*" (catch-all/default) +func (s *Service) Servers() (servers []string) { + s.lock.RLock() + defer s.lock.RUnlock() + for _, m := range s.mappers { + if m.Server == "*" || m.Server == "" { + continue + } + servers = append(servers, m.Server) + } + return servers +} + func (s *Service) mergeLists() (res []UrlMapper) { for _, p := range s.providers { lst, err := p.List() diff --git a/app/discovery/discovery_test.go b/app/discovery/discovery_test.go index 8be8ee8..b441667 100644 --- a/app/discovery/discovery_test.go +++ b/app/discovery/discovery_test.go @@ -127,3 +127,48 @@ func TestService_Match(t *testing.T) { }) } } + +func TestService_Servers(t *testing.T) { + p1 := &ProviderMock{ + EventsFunc: func(ctx context.Context) <-chan struct{} { + res := make(chan struct{}, 1) + res <- struct{}{} + return res + }, + ListFunc: func() ([]UrlMapper, error) { + return []UrlMapper{ + {SrcMatch: regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1"}, + {Server: "m.example.com", SrcMatch: regexp.MustCompile("^/api/svc2/(.*)"), + Dst: "http://127.0.0.2:8080/blah2/$1/abc"}, + }, nil + }, + IDFunc: func() ProviderID { + return PIFile + }, + } + p2 := &ProviderMock{ + EventsFunc: func(ctx context.Context) <-chan struct{} { + return make(chan struct{}, 1) + }, + ListFunc: func() ([]UrlMapper, error) { + return []UrlMapper{ + {Server: "xx.reproxy.io", SrcMatch: regexp.MustCompile("/api/svc3/xyz"), Dst: "http://127.0.0.3:8080/blah3/xyz"}, + }, nil + }, + IDFunc: func() ProviderID { + return PIDocker + }, + } + + ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) + defer cancel() + svc := NewService([]Provider{p1, p2}) + err := svc.Run(ctx) + require.Error(t, err) + assert.Equal(t, context.DeadlineExceeded, err) + assert.Equal(t, 3, len(svc.mappers)) + + servers := svc.Servers() + assert.Equal(t, []string{"m.example.com", "xx.reproxy.io"}, servers) + +} diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index 1021795..a8e5f9f 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -37,6 +37,7 @@ type Http struct { // If no match found return ok=false type Matcher interface { Match(srv, src string) (string, bool) + Servers() (servers []string) } // Run the lister and request's router, activate rest server @@ -72,6 +73,7 @@ func (h *Http) Run(ctx context.Context) error { h.gzipHandler(), ) + h.SSLConfig.FQDNs = h.Servers() // fill all servers switch h.SSLConfig.SSLMode { case SSLNone: log.Printf("[INFO] activate http proxy server on %s", h.Address)