test default host overrid

This commit is contained in:
yusing 2024-03-09 21:22:33 +00:00
parent 5a18dbc34b
commit e736fe1f1e
4 changed files with 12 additions and 2 deletions

Binary file not shown.

2
go.mod
View file

@ -2,7 +2,7 @@ module github.com/yusing/go-proxy
go 1.21.7
require github.com/docker/docker v25.0.3+incompatible
require github.com/docker/docker v25.0.4+incompatible
require github.com/golang/glog v1.2.0

2
go.sum
View file

@ -12,6 +12,8 @@ github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/docker v25.0.3+incompatible h1:D5fy/lYmY7bvZa0XTZ5/UJPljor41F+vdyJG5luQLfQ=
github.com/docker/docker v25.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v25.0.4+incompatible h1:XITZTrq+52tZyZxUOtFIahUf3aH367FLxJzt9vZeAF8=
github.com/docker/docker v25.0.4+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=

View file

@ -16,6 +16,7 @@ import (
)
var dockerClient *client.Client
var defaultHost = os.Getenv("DEFAULT_HOST")
func buildContainerRoute(container types.Container) {
var aliases []string
@ -44,7 +45,12 @@ func buildContainerRoute(container types.Container) {
prop.Set(reflect.ValueOf(value))
}
}
if config.Port == "" {
if config.Port == "" && defaultHost != "" {
for _, port := range container.Ports {
config.Port = fmt.Sprintf("%d", port.PublicPort)
break
}
} else if config.Port == "" {
// usually the smaller port is the http one
// so make it the last one to be set (if 80 or 8080 are not exposed)
sort.Slice(container.Ports, func(i, j int) bool {
@ -87,6 +93,8 @@ func buildContainerRoute(container types.Container) {
}
if config.Host == "" {
switch {
case defaultHost != "":
config.Host = defaultHost
case container.HostConfig.NetworkMode == "host":
config.Host = "host.docker.internal"
case config.LoadBalance == "true":