allow @ substitution in addtion to $ in destination

This commit is contained in:
Umputun
2021-04-11 21:01:52 -05:00
parent a4329dee99
commit 3bf88c5af3
2 changed files with 12 additions and 6 deletions

View File

@@ -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{

View File

@@ -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)
})
}