简体   繁体   中英

Golang ReverseProxy with Iris-Go framework

Anybody able to wire Golang's ReverseProxy functionality with Iris-Go web framework. I couldnt make it working. I am able to wire it with regular 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}
}

How can I use this ReverseProxy object with iris framework;

After searching and posting at Iris i got this example in iris sources. May be helpful for others

https://github.com/kataras/iris/blob/master/http.go#L1412

With iris you have two options, make a proxy server and run it:

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.

create a new proxy handler and use it anywhere 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM