使用Iris-Go框架的Golang ReverseProxy

时间:2016-12-14 12:46:11

标签: go reverse-proxy go-iris

任何人都能够将Golang的ReverseProxy功能与Iris-Go Web框架连接起来。我无法让它发挥作用。我可以用普通的net / http连接它。

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对象与iris框架一起使用;

2 个答案:

答案 0 :(得分:0)

在Iris搜索并发布后,我在虹膜来源中得到了这个例子。可能对其他人有帮助

.each()

答案 1 :(得分:0)

使用iris,您有两个选项,创建一个代理服务器并运行它:

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)