2016-12-14 212 views
0

任何人都可以將Golang的ReverseProxy功能與Iris-Go Web框架聯繫起來。我不能讓它工作。我可以用普通的net/http連接它。帶有Iris-Go框架的Golang ReverseProxy

func MultiHostReverseProxy(targets map[string]utils.Service) *httputil.ReverseProxy { 
    r := regexp.MustCompile(`\/proxy/(?P<Service>[a-zA-Z_-]*)(?P<Path>\/.*)`) 
    director := func(req *http.Request) { 
     if strings.HasPrefix(req.URL.Path, "/proxy/") { 
      temp := r.FindStringSubmatch(req.URL.Path); 
      if (len(temp) > 1) { 
       system := temp[1] 
       if val, ok := targets[system]; ok { 
        s := val.Host + ":" + val.Port 
        req.URL.Scheme = val.Scheme 
        req.URL.Host = s 
        req.URL.Path = temp[2] 

        if enc, ok := GetAxleHeader(req.Header); ok { 
         dec := utils.Decrypt(KEY, enc) 
         req.Header.Set(val.AuthHeader, dec) 
         req.Header.Set(AXLE_HEADER, "") 
        } else { 
         token, nq := utils.FindAxleToken(req.URL.RawQuery); 
         fmt.Printf("%s -> token : %s newQuery: %s\n", req.URL.RawQuery, token, nq); 
         if token != "" { 
          req.URL.RawQuery = nq 
          dec := utils.Decrypt(KEY, token) 
          req.Header.Set(val.AuthHeader, dec) 
          req.Header.Set(AXLE_HEADER, "") 
         } 
        } 
       } 
      } 
     } 
    } 
    return &httputil.ReverseProxy{Director: director} 
} 

如何在虹膜框架中使用此ReverseProxy對象;

+1

我不明白爲什麼有人使用虹膜http://www.florinpatan.ro/2016/10/why-you-should-not-use-iris-for-your-go.html?m=1 – JimB

+0

你不能使用httputi l反向代理,因爲iris不使用std lib net/http軟件包。 – JimB

+0

@JimB在版本5之後,Iris構建在net/http之上,以完成其HTTP/2功能。 – kataras

回答

0

有了你有兩個選擇光圈樂於助人,做一個代理服務器,並運行它:

import "github.com/kataras/iris/core/host" 
[...] 
target, _ := url.Parse("https://example.com") 
go host.NewProxy("example.com:80", target).ListenAndServe() 
// this will proxy all http://example.com to https://example.com 
// you can use that proxy as you like. 

創建一個新的代理處理和使用您想要的位置:

import "github.com/kataras/iris/core/host" 
[...] 
target, _ := url.Parse("https://example.com") 
proxy := host.ProxyHandler(target) 
http.ListenAndServe("example.com:80", proxy) 
+0

謝謝...我一直跟着你的回購,所以知道... – Ysak

+0

有沒有辦法做動態代理,意味着,我不能配置我的代理服務器,而不是它的傳入請求頭(說X轉發)。所以這個請求我應該轉發到那裏提到的相應的服務器... – Ysak