1
0
mirror of https://github.com/umputun/reproxy.git synced 2024-11-24 08:12:31 +02:00

allow docker to provide static with proxy at the same time #27

This commit is contained in:
Umputun 2021-04-17 02:17:23 -05:00
parent 6ad288afd0
commit ee4f8919e1
3 changed files with 32 additions and 27 deletions

View File

@ -103,7 +103,12 @@ func (s *Service) Run(ctx context.Context) error {
evRecv = false evRecv = false
lst := s.mergeLists() lst := s.mergeLists()
for _, m := range lst { 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.lock.Lock()
s.mappers = make(map[string][]URLMapper) s.mappers = make(map[string][]URLMapper)
@ -121,31 +126,21 @@ func (s *Service) Match(srv, src string) (string, MatchType, bool) {
s.lock.RLock() s.lock.RLock()
defer s.lock.RUnlock() defer s.lock.RUnlock()
var staticRules []URLMapper
for _, srvName := range []string{srv, "*", ""} { for _, srvName := range []string{srv, "*", ""} {
for _, m := range s.mappers[srvName] { 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 switch m.MatchType {
// static rule returns a pair (separated by :) of assets location:assets web root case MTProxy:
for _, m := range staticRules { dest := m.SrcMatch.ReplaceAllString(src, m.Dst)
dest := m.SrcMatch.ReplaceAllString(src, m.Dst) if src != dest {
if src == dest { // try to match with trialing / to match web root requests, i.e. /web (without trailing /) return dest, m.MatchType, true
dest := m.SrcMatch.ReplaceAllString(src+"/", m.Dst) }
if src+"/" == dest { case MTStatic:
continue 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 return src, MTProxy, false
@ -204,6 +199,11 @@ func (s *Service) extendMapper(m URLMapper) URLMapper {
src := m.SrcMatch.String() src := m.SrcMatch.String()
m.Dst = strings.Replace(m.Dst, "@", "$", -1) // allow group defined as @n instead of $n (yaml friendly) 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 == "" { if m.MatchType == MTStatic && m.AssetsWebRoot == "" && m.AssetsLocation == "" {
m.AssetsWebRoot = strings.TrimSuffix(src, "/") m.AssetsWebRoot = strings.TrimSuffix(src, "/")
m.AssetsLocation = strings.TrimSuffix(m.Dst, "/") + "/" m.AssetsLocation = strings.TrimSuffix(m.Dst, "/") + "/"

View File

@ -69,7 +69,7 @@ func (d *Docker) List() ([]discovery.URLMapper, error) {
res := make([]discovery.URLMapper, 0, len(containers)) res := make([]discovery.URLMapper, 0, len(containers))
for _, c := range containers { for _, c := range containers {
enabled := false enabled, explicit := false, false
srcURL := "^/(.*)" srcURL := "^/(.*)"
if d.AutoAPI { if d.AutoAPI {
enabled = true 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 // we don't care about value because disabled will be filtered before
if _, ok := c.Labels["reproxy.enabled"]; ok { if _, ok := c.Labels["reproxy.enabled"]; ok {
enabled = true enabled, explicit = true, true
} }
if v, ok := c.Labels["reproxy.route"]; ok { if v, ok := c.Labels["reproxy.route"]; ok {
enabled = true enabled, explicit = true, true
srcURL = v srcURL = v
} }
if v, ok := c.Labels["reproxy.dest"]; ok { 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) 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.AssetsWebRoot = assetsWebRoot
mp.AssetsLocation = assetsLocation mp.AssetsLocation = assetsLocation
} }
res = append(res, mp) 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)
}
} }
} }

View File

@ -219,7 +219,7 @@ func (h *Http) proxyHandler() http.HandlerFunc {
} }
fs, err := R.FileServer(ae[0], ae[1]) fs, err := R.FileServer(ae[0], ae[1])
if err != nil { if err != nil {
http.Error(w, "Server error", http.StatusBadGateway) http.Error(w, "Server error", http.StatusInternalServerError)
return return
} }
fs.ServeHTTP(w, r) fs.ServeHTTP(w, r)