當前位置: 首頁>>代碼示例>>Golang>>正文


Golang ReverseProxy.Director方法代碼示例

本文整理匯總了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)
	}
}
開發者ID:jasonmichels,項目名稱:sage,代碼行數:33,代碼來源:app.go

示例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 = ""
	}
}
開發者ID:jrallison,項目名稱:oauth2_proxy,代碼行數:9,代碼來源:oauthproxy.go

示例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 = ""
	}
}
開發者ID:jrallison,項目名稱:oauth2_proxy,代碼行數:10,代碼來源:oauthproxy.go

示例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))
}
開發者ID:danzilio,項目名稱:meta-graphite,代碼行數:21,代碼來源:main.go


注:本文中的net/http/httputil.ReverseProxy.Director方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。