本文整理汇总了Golang中net/http/httputil.ReverseProxy.Director方法的典型用法代码示例。如果您正苦于以下问题:Golang ReverseProxy.Director方法的具体用法?Golang ReverseProxy.Director怎么用?Golang ReverseProxy.Director使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http/httputil.ReverseProxy
的用法示例。
在下文中一共展示了ReverseProxy.Director方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
// get /blog => to server1
// get /login => to server2
var target1, target2 *url.URL
var err error
if target1, err = url.Parse(SERVER1); err != nil {
log.Fatal("parse url: ", err)
}
if target2, err = url.Parse(SERVER2); err != nil {
log.Fatal("parse url: ", err)
}
reverseProxy := new(httputil.ReverseProxy)
reverseProxy.Director = func(req *http.Request) {
req.URL.Scheme = "http"
if strings.HasPrefix(req.URL.Path, "/blog") {
req.URL.Host = target1.Host
}
if strings.HasPrefix(req.URL.Path, "/login") {
req.URL.Host = target2.Host
}
}
err = http.ListenAndServe(":80", reverseProxy)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
示例2: setProxyDirector
func setProxyDirector(proxy *httputil.ReverseProxy) {
director := proxy.Director
proxy.Director = func(req *http.Request) {
director(req)
// use RequestURI so that we aren't unescaping encoded slashes in the request path
req.URL.Opaque = req.RequestURI
req.URL.RawQuery = ""
}
}
示例3: setProxyUpstreamHostHeader
func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
director := proxy.Director
proxy.Director = func(req *http.Request) {
director(req)
// use RequestURI so that we aren't unescaping encoded slashes in the request path
req.Host = target.Host
req.URL.Opaque = req.RequestURI
req.URL.RawQuery = ""
}
}
示例4: main
func main() {
var srv httputil.ReverseProxy
log.SetFlags(0)
flag.Parse()
if *file == "" {
log.Print("config file (-c) is required")
flag.PrintDefaults()
os.Exit(1)
}
if cfg, err := config.ParseFile(*file); err != nil {
log.Fatalf("parse %s failed: %s", *file, err)
} else {
srv.Director = cfg.MapRequest
if *addr == "" {
*addr = cfg.Address
}
}
http.Handle("/render", accesslog.Handler(&srv, nil))
log.Fatal(http.ListenAndServe(*addr, nil))
}