本文整理匯總了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))
}