mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 17:28:53 +02:00
added explicit only mode for docker provider, updated dependencies
This commit is contained in:
@@ -42,7 +42,7 @@ func NewHandler(cfg *config.Config) http.Handler {
|
||||
func checkHost(f http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Host != common.APIHTTPAddr {
|
||||
Logger.Warnf("invalid request to API server with host: %s, expected: %s", r.Host, common.APIHTTPAddr)
|
||||
Logger.Warnf("invalid request to API server with host: %s, expect %s", r.Host, common.APIHTTPAddr)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("invalid request"))
|
||||
return
|
||||
|
||||
@@ -33,7 +33,7 @@ type Config struct {
|
||||
|
||||
var instance *Config
|
||||
|
||||
func GetConfig() *Config {
|
||||
func GetInstance() *Config {
|
||||
return instance
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ type Container struct {
|
||||
|
||||
func FromDocker(c *types.Container, dockerHost string) (res Container) {
|
||||
res.Container = c
|
||||
isExplicit := c.Labels[LabelAliases] != ""
|
||||
res.ProxyProperties = &ProxyProperties{
|
||||
DockerHost: dockerHost,
|
||||
ContainerName: res.getName(),
|
||||
@@ -25,6 +26,7 @@ func FromDocker(c *types.Container, dockerHost string) (res Container) {
|
||||
NetworkMode: c.HostConfig.NetworkMode,
|
||||
Aliases: res.getAliases(),
|
||||
IsExcluded: U.ParseBool(res.getDeleteLabel(LabelExclude)),
|
||||
IsExplicit: isExplicit,
|
||||
IdleTimeout: res.getDeleteLabel(LabelIdleTimeout),
|
||||
WakeTimeout: res.getDeleteLabel(LabelWakeTimeout),
|
||||
StopMethod: res.getDeleteLabel(LabelStopMethod),
|
||||
|
||||
@@ -13,6 +13,7 @@ type ProxyProperties struct {
|
||||
|
||||
Aliases []string `yaml:"-" json:"aliases"`
|
||||
IsExcluded bool `yaml:"-" json:"is_excluded"`
|
||||
IsExplicit bool `yaml:"-" json:"is_explicit"`
|
||||
IdleTimeout string `yaml:"-" json:"idle_timeout"`
|
||||
WakeTimeout string `yaml:"-" json:"wake_timeout"`
|
||||
StopMethod string `yaml:"-" json:"stop_method"`
|
||||
|
||||
@@ -3,6 +3,7 @@ package model
|
||||
type Config struct {
|
||||
Providers ProxyProviders `yaml:",flow" json:"providers"`
|
||||
AutoCert AutoCertConfig `yaml:",flow" json:"autocert"`
|
||||
ExplicitOnly bool `yaml:"explicit_only" json:"explicit_only"`
|
||||
MatchDomains []string `yaml:"match_domains" json:"match_domains"`
|
||||
TimeoutShutdown int `yaml:"timeout_shutdown" json:"timeout_shutdown"`
|
||||
RedirectToHTTPS bool `yaml:"redirect_to_https" json:"redirect_to_https"`
|
||||
|
||||
@@ -16,17 +16,18 @@ import (
|
||||
|
||||
type DockerProvider struct {
|
||||
dockerHost, hostname string
|
||||
ExplicitOnly bool
|
||||
}
|
||||
|
||||
var AliasRefRegex = regexp.MustCompile(`#\d+`)
|
||||
var AliasRefRegexOld = regexp.MustCompile(`\$\d+`)
|
||||
|
||||
func DockerProviderImpl(dockerHost string) (ProviderImpl, E.NestedError) {
|
||||
func DockerProviderImpl(dockerHost string, explicitOnly bool) (ProviderImpl, E.NestedError) {
|
||||
hostname, err := D.ParseDockerHostname(dockerHost)
|
||||
if err.HasError() {
|
||||
return nil, err
|
||||
}
|
||||
return &DockerProvider{dockerHost: dockerHost, hostname: hostname}, nil
|
||||
return &DockerProvider{dockerHost, hostname, explicitOnly}, nil
|
||||
}
|
||||
|
||||
func (p *DockerProvider) String() string {
|
||||
@@ -122,11 +123,12 @@ func (p *DockerProvider) OnEvent(event W.Event, routes R.Routes) (res EventResul
|
||||
|
||||
// Returns a list of proxy entries for a container.
|
||||
// Always non-nil
|
||||
func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (M.RawEntries, E.NestedError) {
|
||||
entries := M.NewProxyEntries()
|
||||
func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entries M.RawEntries, _ E.NestedError) {
|
||||
entries = M.NewProxyEntries()
|
||||
|
||||
if container.IsExcluded {
|
||||
return entries, nil
|
||||
if container.IsExcluded ||
|
||||
!container.IsExplicit && p.ExplicitOnly {
|
||||
return
|
||||
}
|
||||
|
||||
// init entries map for all aliases
|
||||
|
||||
@@ -56,6 +56,9 @@ func newProvider(name string, t ProviderType) *Provider {
|
||||
|
||||
func NewFileProvider(filename string) (p *Provider, err E.NestedError) {
|
||||
name := path.Base(filename)
|
||||
if name == "" {
|
||||
return nil, E.Invalid("file name", "empty")
|
||||
}
|
||||
p = newProvider(name, ProviderTypeFile)
|
||||
p.ProviderImpl, err = FileProviderImpl(filename)
|
||||
if err != nil {
|
||||
@@ -66,8 +69,17 @@ func NewFileProvider(filename string) (p *Provider, err E.NestedError) {
|
||||
}
|
||||
|
||||
func NewDockerProvider(name string, dockerHost string) (p *Provider, err E.NestedError) {
|
||||
if name == "" {
|
||||
return nil, E.Invalid("provider name", "empty")
|
||||
}
|
||||
explicitOnly := false
|
||||
if name[len(name)-1] == '!' {
|
||||
explicitOnly = true
|
||||
name = name[:len(name)-1]
|
||||
}
|
||||
|
||||
p = newProvider(name, ProviderTypeDocker)
|
||||
p.ProviderImpl, err = DockerProviderImpl(dockerHost)
|
||||
p.ProviderImpl, err = DockerProviderImpl(dockerHost, explicitOnly)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user