support groups of route for file provider

This commit is contained in:
Umputun 2021-04-05 01:37:05 -05:00
parent dfa0d51b31
commit 75fddfa7fe
3 changed files with 20 additions and 14 deletions

View File

@ -66,12 +66,10 @@ func (d *File) Events(ctx context.Context) <-chan struct{} {
// List all src dst pairs
func (d *File) List() (res []discovery.UrlMapper, err error) {
var fileConf []struct {
SourceServer string `yaml:"server"`
SourceRoute string `yaml:"route"`
Dest string `yaml:"dest"`
var fileConf map[string][]struct {
SourceRoute string `yaml:"route"`
Dest string `yaml:"dest"`
}
fh, err := os.Open(d.FileName)
if err != nil {
return nil, errors.Wrapf(err, "can't open %s", d.FileName)
@ -83,12 +81,17 @@ func (d *File) List() (res []discovery.UrlMapper, err error) {
}
log.Printf("[DEBUG] file provider %+v", res)
for _, f := range fileConf {
rx, err := regexp.Compile(f.SourceRoute)
if err != nil {
return nil, errors.Wrapf(err, "can't parse regex %s", f.SourceRoute)
for srv, fl := range fileConf {
for _, f := range fl {
rx, err := regexp.Compile(f.SourceRoute)
if err != nil {
return nil, errors.Wrapf(err, "can't parse regex %s", f.SourceRoute)
}
if srv == "default" {
srv = "*"
}
res = append(res, discovery.UrlMapper{Server: srv, SrcMatch: rx, Dst: f.Dest})
}
res = append(res, discovery.UrlMapper{Server: f.SourceServer, SrcMatch: rx, Dst: f.Dest})
}
return res, nil
}

View File

@ -59,5 +59,6 @@ func TestFile_List(t *testing.T) {
t.Logf("%+v", res)
assert.Equal(t, 3, len(res))
assert.Equal(t, "^/api/svc1/(.*)", res[0].SrcMatch.String())
assert.Equal(t, "http://127.0.0.2:8080/blah2/$1/abc", res[1].Dst)
assert.Equal(t, "http://127.0.0.3:8080/blah3/xyz", res[1].Dst)
assert.Equal(t, "http://127.0.0.2:8080/blah2/$1/abc", res[2].Dst)
}

View File

@ -1,3 +1,5 @@
- {server: "*", route: "^/api/svc1/(.*)", dest: "http://127.0.0.1:8080/blah1/$1"}
- {server: "srv.example.com", route: "^/api/svc2/(.*)", dest: "http://127.0.0.2:8080/blah2/$1/abc"}
- {server: "*", route: "/api/svc3/xyz", dest: "http://127.0.0.3:8080/blah3/xyz"}
default:
- {route: "^/api/svc1/(.*)", dest: "http://127.0.0.1:8080/blah1/$1"}
- {route: "/api/svc3/xyz", dest: "http://127.0.0.3:8080/blah3/xyz"}
srv.example.com:
- {route: "^/api/svc2/(.*)", dest: "http://127.0.0.2:8080/blah2/$1/abc"}