proxy add header process
This commit is contained in:
parent
7f32c1a38f
commit
33c988a952
|
@ -31,12 +31,13 @@ func (p *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// init httputil.ReverseProxy instance and add path rewrite and add session-sticky cookie to response
|
||||
func makeProxy(upstream string, pw model.PathRewrite, index int) *httputil.ReverseProxy {
|
||||
func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.ReverseProxy {
|
||||
p := &httputil.ReverseProxy{}
|
||||
p.Director = func(req *http.Request) {
|
||||
turl, _ := url.Parse(upstream)
|
||||
req.URL.Host = turl.Host
|
||||
req.URL.Scheme = turl.Scheme
|
||||
pw := path.Rewrite
|
||||
if pw.Replace != "" {
|
||||
req.URL.Path = strings.TrimPrefix(req.URL.Path, pw.Replace)
|
||||
if req.URL.RawPath != "" {
|
||||
|
@ -49,6 +50,20 @@ func makeProxy(upstream string, pw model.PathRewrite, index int) *httputil.Rever
|
|||
req.URL.RawPath = strings.TrimSuffix(pw.With, "/") + "/" + req.URL.RawPath
|
||||
}
|
||||
}
|
||||
|
||||
if len(path.Headers) > 0 {
|
||||
for _, header := range path.Headers {
|
||||
//req.Header.Add(header.Name, header.Value)
|
||||
value := ""
|
||||
switch header.Value {
|
||||
case string(model.ProxyHost):
|
||||
value = turl.Host
|
||||
default:
|
||||
value = header.Value
|
||||
}
|
||||
req.Header.Set(header.Name, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p.ModifyResponse = func(resp *http.Response) error {
|
||||
|
@ -80,7 +95,7 @@ func NewProxyHandler(p *model.HttpPath) *ProxyHandler {
|
|||
ph.proxy = make([]*httputil.ReverseProxy, upstreamCount)
|
||||
|
||||
for index, upstream := range p.Upstreams {
|
||||
ph.proxy[index] = makeProxy(upstream, p.Rewrite, index)
|
||||
ph.proxy[index] = makeProxy(upstream, p, index)
|
||||
}
|
||||
return ph
|
||||
}
|
||||
|
|
|
@ -9,11 +9,12 @@ import (
|
|||
)
|
||||
|
||||
type HttpPath struct {
|
||||
Path string `json:"path"`
|
||||
Root string `json:"root"`
|
||||
Default string `json:"default"`
|
||||
Upstreams []string `json:"upstreams"`
|
||||
Rewrite PathRewrite `json:"pathrewrite"`
|
||||
Path string `json:"path"`
|
||||
Root string `json:"root"`
|
||||
Default string `json:"default"`
|
||||
Upstreams []string `json:"upstreams"`
|
||||
Rewrite PathRewrite `json:"pathrewrite"`
|
||||
Headers []HeaderDefine `json:"headers"`
|
||||
}
|
||||
|
||||
type PathRewrite struct {
|
||||
|
@ -21,6 +22,17 @@ type PathRewrite struct {
|
|||
With string `json:"with"`
|
||||
}
|
||||
|
||||
type HeaderDefine struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type HeaderValueConst string
|
||||
|
||||
const (
|
||||
ProxyHost HeaderValueConst = "$ProxyHost"
|
||||
)
|
||||
|
||||
type HttpServerConfig struct {
|
||||
Name string `json:"name"`
|
||||
ServerName string `json:"server"`
|
||||
|
|
Loading…
Reference in New Issue