相关文档
样例代码
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
func NewReverseProxy(target *url.URL) *httputil.ReverseProxy {
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
log.Printf("req %+v \n", req)
}
modifyresponse := func(response *http.Response) error {
response.Header.Set("Access-Control-Allow-Origin", "*")
response.Header.Set("Access-Control-Allow-Methods", "*")
response.Header.Set("Access-Control-Allow-Headers", "*")
log.Printf("res %+v \n", response)
return nil
}
return &httputil.ReverseProxy{
Director: director,
ModifyResponse: modifyresponse,
}
}
func main() {
addr := ":8080"
proxy := NewReverseProxy(&url.URL{
Scheme: "http",
Host: "target.com",
})
log.Printf("listen at [%s]\n", addr)
log.Fatal(http.ListenAndServe(addr, proxy))
}
版权属于:谁把年华错落成诗 所有,转载请注明出处!
本文链接:https://blog.pomears.com/archives/66.html
如果博客部分链接出现404,请留言或者联系博主修复。