forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_target_proxy_handler.go
96 lines (84 loc) · 2.85 KB
/
multi_target_proxy_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"errors"
"github.com/Sirupsen/logrus"
"io"
"net/http"
"net/url"
)
type MultiTargetProxy struct {
VersionProxyMap map[string]*ReverseProxy
specReference *APISpec
defaultProxy *ReverseProxy
}
func (m *MultiTargetProxy) getProxyForRequest(r *http.Request) (*ReverseProxy, error) {
thisVersion, _, _, _ := m.specReference.GetVersionData(r)
proxy, found := m.VersionProxyMap[thisVersion.Name]
if !found {
return nil, errors.New("Proxy not found!")
}
return proxy, nil
}
func (m *MultiTargetProxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) *http.Response {
log.WithFields(logrus.Fields{
"prefix": "multi-target",
}).Debug("Serving Multi-target...")
thisProxy, err := m.getProxyForRequest(r)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "multi-target",
}).Warning("No proxy found, using default")
return m.defaultProxy.ServeHTTP(rw, r)
}
return thisProxy.ServeHTTP(rw, r)
}
func (m *MultiTargetProxy) ServeHTTPForCache(rw http.ResponseWriter, r *http.Request) *http.Response {
thisProxy, err := m.getProxyForRequest(r)
if err != nil {
return m.defaultProxy.ServeHTTPForCache(rw, r)
}
return thisProxy.ServeHTTPForCache(rw, r)
}
func (m *MultiTargetProxy) CopyResponse(dst io.Writer, src io.Reader) {
m.defaultProxy.CopyResponse(dst, src)
}
func (m *MultiTargetProxy) New(c interface{}, spec *APISpec) (TykResponseHandler, error) {
m.VersionProxyMap = make(map[string]*ReverseProxy)
m.specReference = spec
remote, err := url.Parse(spec.Proxy.TargetURL)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "multi-target",
}).Error("Couldn't parse default target URL in MultiTarget: ", err)
}
m.defaultProxy = TykNewSingleHostReverseProxy(remote, spec)
m.defaultProxy.New(nil, spec)
for versionName, versionData := range spec.VersionData.Versions {
if versionData.OverrideTarget == "" {
log.WithFields(logrus.Fields{
"prefix": "multi-target",
}).Info("----> Version ", versionName, " has no override target")
m.VersionProxyMap[versionName] = m.defaultProxy
} else {
versionRemote, vRemoteErr := url.Parse(versionData.OverrideTarget)
log.WithFields(logrus.Fields{
"prefix": "multi-target",
}).Info("----> Version ", versionName, " has '", versionData.OverrideTarget, "' for override target")
log.WithFields(logrus.Fields{
"prefix": "multi-target",
}).Debug("Multi-target URL: ", versionData.OverrideTarget)
log.WithFields(logrus.Fields{
"prefix": "multi-target",
}).Debug("Multi-target URL (obj): ", versionRemote)
if vRemoteErr != nil {
log.WithFields(logrus.Fields{
"prefix": "multi-target",
}).Error("Couldn't parse version target URL in MultiTarget: ", vRemoteErr)
}
versionProxy := TykNewSingleHostReverseProxy(versionRemote, spec)
versionProxy.New(nil, spec)
m.VersionProxyMap[versionName] = versionProxy
}
}
return nil, nil
}