From ee4f8919e1390891ca3c1c731fa89768bf496857 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 17 Apr 2021 02:17:23 -0500 Subject: [PATCH] allow docker to provide static with proxy at the same time #27 --- app/discovery/discovery.go | 42 ++++++++++++++++---------------- app/discovery/provider/docker.go | 15 ++++++++---- app/proxy/proxy.go | 2 +- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/app/discovery/discovery.go b/app/discovery/discovery.go index 930aef0..53a3c93 100644 --- a/app/discovery/discovery.go +++ b/app/discovery/discovery.go @@ -103,7 +103,12 @@ func (s *Service) Run(ctx context.Context) error { evRecv = false lst := s.mergeLists() for _, m := range lst { - log.Printf("[INFO] match for %s: %s %s -> %s (%s)", m.ProviderID, m.Server, m.SrcMatch.String(), m.Dst, m.MatchType) + if m.MatchType == MTProxy { + log.Printf("[INFO] proxy %s: %s %s -> %s", m.ProviderID, m.Server, m.SrcMatch.String(), m.Dst) + } + if m.MatchType == MTStatic { + log.Printf("[INFO] assets %s: %s %s -> %s", m.ProviderID, m.Server, m.AssetsWebRoot, m.AssetsLocation) + } } s.lock.Lock() s.mappers = make(map[string][]URLMapper) @@ -121,31 +126,21 @@ func (s *Service) Match(srv, src string) (string, MatchType, bool) { s.lock.RLock() defer s.lock.RUnlock() - var staticRules []URLMapper for _, srvName := range []string{srv, "*", ""} { for _, m := range s.mappers[srvName] { - if m.MatchType == MTStatic { // collect static for - staticRules = append(staticRules, m) - continue - } - dest := m.SrcMatch.ReplaceAllString(src, m.Dst) - if src != dest { - return dest, m.MatchType, true - } - } - } - // process static rules after all regular proxy rules as we want to prioritize regular rules - // static rule returns a pair (separated by :) of assets location:assets web root - for _, m := range staticRules { - dest := m.SrcMatch.ReplaceAllString(src, m.Dst) - if src == dest { // try to match with trialing / to match web root requests, i.e. /web (without trailing /) - dest := m.SrcMatch.ReplaceAllString(src+"/", m.Dst) - if src+"/" == dest { - continue + switch m.MatchType { + case MTProxy: + dest := m.SrcMatch.ReplaceAllString(src, m.Dst) + if src != dest { + return dest, m.MatchType, true + } + case MTStatic: + if src == m.AssetsWebRoot || strings.HasSuffix(src, m.AssetsWebRoot+"/") { + return m.AssetsWebRoot + ":" + m.AssetsLocation, MTStatic, true + } } } - return m.AssetsWebRoot + ":" + m.AssetsLocation, MTStatic, true } return src, MTProxy, false @@ -204,6 +199,11 @@ func (s *Service) extendMapper(m URLMapper) URLMapper { src := m.SrcMatch.String() m.Dst = strings.Replace(m.Dst, "@", "$", -1) // allow group defined as @n instead of $n (yaml friendly) + if m.MatchType == MTStatic && m.AssetsWebRoot != "" && m.AssetsLocation != "" { + m.AssetsWebRoot = strings.TrimSuffix(m.AssetsWebRoot, "/") + m.AssetsLocation = strings.TrimSuffix(m.AssetsLocation, "/") + "/" + } + if m.MatchType == MTStatic && m.AssetsWebRoot == "" && m.AssetsLocation == "" { m.AssetsWebRoot = strings.TrimSuffix(src, "/") m.AssetsLocation = strings.TrimSuffix(m.Dst, "/") + "/" diff --git a/app/discovery/provider/docker.go b/app/discovery/provider/docker.go index a553821..b97e6ab 100644 --- a/app/discovery/provider/docker.go +++ b/app/discovery/provider/docker.go @@ -69,7 +69,7 @@ func (d *Docker) List() ([]discovery.URLMapper, error) { res := make([]discovery.URLMapper, 0, len(containers)) for _, c := range containers { - enabled := false + enabled, explicit := false, false srcURL := "^/(.*)" if d.AutoAPI { enabled = true @@ -89,16 +89,16 @@ func (d *Docker) List() ([]discovery.URLMapper, error) { // we don't care about value because disabled will be filtered before if _, ok := c.Labels["reproxy.enabled"]; ok { - enabled = true + enabled, explicit = true, true } if v, ok := c.Labels["reproxy.route"]; ok { - enabled = true + enabled, explicit = true, true srcURL = v } if v, ok := c.Labels["reproxy.dest"]; ok { - enabled = true + enabled, explicit = true, true destURL = fmt.Sprintf("http://%s:%d%s", c.IP, port, v) } @@ -140,8 +140,13 @@ func (d *Docker) List() ([]discovery.URLMapper, error) { mp.AssetsWebRoot = assetsWebRoot mp.AssetsLocation = assetsLocation } - res = append(res, mp) + + // for assets we add the second proxy mapping only if explicitly requested + if assetsWebRoot != "" && explicit { + mp.MatchType = discovery.MTProxy + res = append(res, mp) + } } } diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index 9ff89ff..3b97998 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -219,7 +219,7 @@ func (h *Http) proxyHandler() http.HandlerFunc { } fs, err := R.FileServer(ae[0], ae[1]) if err != nil { - http.Error(w, "Server error", http.StatusBadGateway) + http.Error(w, "Server error", http.StatusInternalServerError) return } fs.ServeHTTP(w, r)