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:
parent
6ad288afd0
commit
ee4f8919e1
@ -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, "/") + "/"
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user