mirror of
https://github.com/umputun/reproxy.git
synced 2024-09-11 08:55:25 +03:00
support groups of route for file provider
This commit is contained in:
parent
dfa0d51b31
commit
75fddfa7fe
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
8
app/discovery/provider/testdata/config.yml
vendored
8
app/discovery/provider/testdata/config.yml
vendored
@ -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"}
|
||||
|
Loading…
Reference in New Issue
Block a user