mirror of
https://github.com/umputun/reproxy.git
synced 2024-11-24 08:12:31 +02:00
change docker default and add autoapi param #20
This commit is contained in:
parent
1ac2f23504
commit
a20abd0140
@ -26,6 +26,7 @@ type Docker struct {
|
||||
DockerClient DockerClient
|
||||
Excludes []string
|
||||
Network string
|
||||
AutoAPI bool
|
||||
}
|
||||
|
||||
// DockerClient defines interface listing containers and subscribing to events
|
||||
@ -71,7 +72,10 @@ func (d *Docker) List() ([]discovery.URLMapper, error) {
|
||||
|
||||
res := make([]discovery.URLMapper, 0, len(containers))
|
||||
for _, c := range containers {
|
||||
srcURL := fmt.Sprintf("^/api/%s/(.*)", c.Name)
|
||||
srcURL := "^/(.*)"
|
||||
if d.AutoAPI {
|
||||
srcURL = fmt.Sprintf("^/api/%s/(.*)", c.Name)
|
||||
}
|
||||
destURL := fmt.Sprintf("http://%s:%d/$1", c.IP, c.Port)
|
||||
pingURL := fmt.Sprintf("http://%s:%d/ping", c.IP, c.Port)
|
||||
server := "*"
|
||||
|
@ -64,11 +64,70 @@ func TestDocker_List(t *testing.T) {
|
||||
assert.Equal(t, "example.com", res[0].Server)
|
||||
assert.Equal(t, "http://127.0.0.2:12345/ping", res[0].PingURL)
|
||||
|
||||
assert.Equal(t, "^/(.*)", res[1].SrcMatch.String())
|
||||
assert.Equal(t, "http://127.0.0.3:12346/$1", res[1].Dst)
|
||||
assert.Equal(t, "http://127.0.0.3:12346/ping", res[1].PingURL)
|
||||
assert.Equal(t, "*", res[1].Server)
|
||||
}
|
||||
|
||||
func TestDocker_ListWithAutoAPI(t *testing.T) {
|
||||
dclient := &DockerClientMock{
|
||||
ListContainersFunc: func(opts dc.ListContainersOptions) ([]dc.APIContainers, error) {
|
||||
return []dc.APIContainers{
|
||||
{Names: []string{"c1"}, State: "running",
|
||||
Networks: dc.NetworkList{
|
||||
Networks: map[string]dc.ContainerNetwork{"bridge": {IPAddress: "127.0.0.2"}},
|
||||
},
|
||||
Ports: []dc.APIPort{
|
||||
{PrivatePort: 12345},
|
||||
},
|
||||
Labels: map[string]string{"reproxy.route": "^/api/123/(.*)", "reproxy.dest": "/blah/$1",
|
||||
"reproxy.server": "example.com", "reproxy.ping": "/ping"},
|
||||
},
|
||||
{Names: []string{"c2"}, State: "running",
|
||||
Networks: dc.NetworkList{
|
||||
Networks: map[string]dc.ContainerNetwork{"bridge": {IPAddress: "127.0.0.3"}},
|
||||
},
|
||||
Ports: []dc.APIPort{
|
||||
{PrivatePort: 12346},
|
||||
},
|
||||
},
|
||||
{Names: []string{"c3"}, State: "stopped"},
|
||||
{Names: []string{"c4"}, State: "running",
|
||||
Networks: dc.NetworkList{
|
||||
Networks: map[string]dc.ContainerNetwork{"other": {IPAddress: "127.0.0.2"}},
|
||||
},
|
||||
Ports: []dc.APIPort{
|
||||
{PrivatePort: 12345},
|
||||
},
|
||||
},
|
||||
{Names: []string{"c5"}, State: "running",
|
||||
Networks: dc.NetworkList{
|
||||
Networks: map[string]dc.ContainerNetwork{"bridge": {IPAddress: "127.0.0.122"}},
|
||||
},
|
||||
Ports: []dc.APIPort{
|
||||
{PrivatePort: 2345},
|
||||
},
|
||||
Labels: map[string]string{"reproxy.enabled": "false"},
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
d := Docker{DockerClient: dclient, Network: "bridge", AutoAPI: true}
|
||||
res, err := d.List()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, len(res))
|
||||
|
||||
assert.Equal(t, "^/api/123/(.*)", res[0].SrcMatch.String())
|
||||
assert.Equal(t, "http://127.0.0.2:12345/blah/$1", res[0].Dst)
|
||||
assert.Equal(t, "example.com", res[0].Server)
|
||||
assert.Equal(t, "http://127.0.0.2:12345/ping", res[0].PingURL)
|
||||
|
||||
assert.Equal(t, "^/api/c2/(.*)", res[1].SrcMatch.String())
|
||||
assert.Equal(t, "http://127.0.0.3:12346/$1", res[1].Dst)
|
||||
assert.Equal(t, "http://127.0.0.3:12346/ping", res[1].PingURL)
|
||||
assert.Equal(t, "*", res[1].Server)
|
||||
|
||||
}
|
||||
|
||||
func TestDocker_Events(t *testing.T) {
|
||||
|
@ -172,7 +172,11 @@ func makeProviders() ([]discovery.Provider, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to make docker client %s", err)
|
||||
}
|
||||
res = append(res, &provider.Docker{DockerClient: client, Excludes: opts.Docker.Excluded, Network: opts.Docker.Network})
|
||||
if opts.Docker.AutoAPI {
|
||||
log.Printf("[INFO] auto-api enabled for docker")
|
||||
}
|
||||
res = append(res, &provider.Docker{DockerClient: client, Excludes: opts.Docker.Excluded,
|
||||
Network: opts.Docker.Network, AutoAPI: opts.Docker.AutoAPI})
|
||||
}
|
||||
|
||||
if len(res) == 0 && opts.Assets.Location == "" {
|
||||
|
Loading…
Reference in New Issue
Block a user