From 3bf88c5af3ba221dfb197c184302d73b5f545c03 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 11 Apr 2021 21:01:52 -0500 Subject: [PATCH] allow @ substitution in addtion to $ in destination --- app/discovery/discovery.go | 14 ++++++++++---- app/discovery/discovery_test.go | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/discovery/discovery.go b/app/discovery/discovery.go index 43dfb15..b35ead9 100644 --- a/app/discovery/discovery.go +++ b/app/discovery/discovery.go @@ -132,7 +132,7 @@ func (s *Service) mergeLists() (res []URLMapper) { continue } for i := range lst { - lst[i] = s.extendRule(lst[i]) + lst[i] = s.extendMapper(lst[i]) lst[i].ProviderID = p.ID() } res = append(res, lst...) @@ -140,13 +140,19 @@ func (s *Service) mergeLists() (res []URLMapper) { return res } -// extendRule from /something/blah->http://example.com/api to ^/something/blah/(.*)->http://example.com/api/$1 -func (s *Service) extendRule(m URLMapper) URLMapper { +// extendMapper from /something/blah->http://example.com/api to ^/something/blah/(.*)->http://example.com/api/$1 +// also substitutes @ in dest by $. The reason for this substitution - some providers, for example docker +// treat $ in a special way for variable substitution and user has to escape $, like this reproxy.dest: '/$$1' +// It can be simplified with @, i.e. reproxy.dest: '/@1' +func (s *Service) extendMapper(m URLMapper) URLMapper { src := m.SrcMatch.String() // TODO: Probably should be ok in practice but we better figure a nicer way to do it - if strings.Contains(m.Dst, "$1") || strings.Contains(src, "(") || !strings.HasSuffix(src, "/") { + if strings.Contains(m.Dst, "$1") || strings.Contains(m.Dst, "@1") || + strings.Contains(src, "(") || !strings.HasSuffix(src, "/") { + + m.Dst = strings.Replace(m.Dst, "@", "$", -1) // allow group defined as @n instead of $n return m } res := URLMapper{ diff --git a/app/discovery/discovery_test.go b/app/discovery/discovery_test.go index c7e0f4c..3b268df 100644 --- a/app/discovery/discovery_test.go +++ b/app/discovery/discovery_test.go @@ -21,7 +21,7 @@ func TestService_Do(t *testing.T) { ListFunc: func() ([]URLMapper, error) { return []URLMapper{ {Server: "*", SrcMatch: *regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1"}, - {Server: "*", SrcMatch: *regexp.MustCompile("^/api/svc2/(.*)"), Dst: "http://127.0.0.2:8080/blah2/$1/abc"}, + {Server: "*", SrcMatch: *regexp.MustCompile("^/api/svc2/(.*)"), Dst: "http://127.0.0.2:8080/blah2/@1/abc"}, }, nil }, IDFunc: func() ProviderID { @@ -209,7 +209,7 @@ func TestService_extendRule(t *testing.T) { for i, tt := range tbl { tt := tt t.Run(strconv.Itoa(i), func(t *testing.T) { - res := svc.extendRule(tt.inp) + res := svc.extendMapper(tt.inp) assert.Equal(t, tt.out, res) }) }