http代理在beego框架的应用小例
定义一个handle结构体
typehandlestruct{
}
定义代理函数
func(this*handle)ServeHTTP(whttp.ResponseWriter,r*http.Request){
address:=beego.AppConfig.String("addr")
defaultPrefix:="http://"
remote,err:=url.Parse(defaultPrefix+address)
if err!=nil{
panic(err)
}
proxy:=httputil.NewSingleHostReverseProxy(remote)
varpTransporthttp.RoundTripper=&http.Transport{
Proxy:http.ProxyFromEnvironment,
Dial:(&net.Dialer{
Timeout:30*time.Second,
KeepAlive:30*time.Second,
}).Dial,
MaxIdleConns:1000,
IdleConnTimeout:10*time.Second,
TLSHandshakeTimeout:10*time.Second,
ExpectContinueTimeout:1*time.Second,
DisableKeepAlives:false,
}
proxy.Transport=pTransport
proxy.ServeHTTP(w,r)
}
路由设置
vars=&handle{}
beego.Handler("/*",s)