import("log""net/http""net/http/httputil""net/url")// NewProxy takes target host and creates a reverse proxy
// NewProxy 拿到 targetHost 后,创建一个反向代理
funcNewProxy(targetHoststring)(*httputil.ReverseProxy,error){url,err:=url.Parse(targetHost)iferr!=nil{returnnil,err}returnhttputil.NewSingleHostReverseProxy(url),nil}// ProxyRequestHandler handles the http request using proxy
// ProxyRequestHandler 使用 proxy 处理请求
funcProxyRequestHandler(proxy*httputil.ReverseProxy)func(http.ResponseWriter,*http.Request){returnfunc(whttp.ResponseWriter,r*http.Request){proxy.ServeHTTP(w,r)}}funcmain(){// initialize a reverse proxy and pass the actual backend server url here
// 初始化反向代理并传入真正后端服务的地址
proxy,err:=NewProxy("http://my-api-server.com")iferr!=nil{panic(err)}// handle all requests to your server using the proxy
// 使用 proxy 处理所有请求到你的服务
http.HandleFunc("/",ProxyRequestHandler(proxy))log.Fatal(http.ListenAndServe(":8080",nil))}
是的没错!这就是在 Go 中创建一个简单的反向代理所需的全部内容。
我们使用标准库 net/http/httputil 创建了一个单主机的反向代理。
到达我们代理服务器的任何请求都会被代理到位于 http://my-api-server.com。
如果你对 Go 比较熟悉,这个代码的实现一目了然。
// NewProxy takes target host and creates a reverse proxy
funcNewProxy(targetHoststring)(*httputil.ReverseProxy,error){url,err:=url.Parse(targetHost)iferr!=nil{returnnil,err}proxy:=httputil.NewSingleHostReverseProxy(url)proxy.ModifyResponse=modifyResponse()proxy.ErrorHandler=errorHandler()returnproxy,nil}funcerrorHandler()func(http.ResponseWriter,*http.Request,error){returnfunc(whttp.ResponseWriter,req*http.Request,errerror){fmt.Printf("Got error while modifying response: %v \n",err)return}}funcmodifyResponse()func(*http.Response)error{returnfunc(resp*http.Response)error{returnerrors.New("response body is invalid")}}
packagemainimport("errors""fmt""log""net/http""net/http/httputil""net/url")// NewProxy takes target host and creates a reverse proxy
funcNewProxy(targetHoststring)(*httputil.ReverseProxy,error){url,err:=url.Parse(targetHost)iferr!=nil{returnnil,err}proxy:=httputil.NewSingleHostReverseProxy(url)originalDirector:=proxy.Directorproxy.Director=func(req*http.Request){originalDirector(req)modifyRequest(req)}proxy.ModifyResponse=modifyResponse()proxy.ErrorHandler=errorHandler()returnproxy,nil}funcmodifyRequest(req*http.Request){req.Header.Set("X-Proxy","Simple-Reverse-Proxy")}funcerrorHandler()func(http.ResponseWriter,*http.Request,error){returnfunc(whttp.ResponseWriter,req*http.Request,errerror){fmt.Printf("Got error while modifying response: %v \n",err)return}}funcmodifyResponse()func(*http.Response)error{returnfunc(resp*http.Response)error{returnerrors.New("response body is invalid")}}// ProxyRequestHandler handles the http request using proxy
funcProxyRequestHandler(proxy*httputil.ReverseProxy)func(http.ResponseWriter,*http.Request){returnfunc(whttp.ResponseWriter,r*http.Request){proxy.ServeHTTP(w,r)}}funcmain(){// initialize a reverse proxy and pass the actual backend server url here
proxy,err:=NewProxy("http://my-api-server.com")iferr!=nil{panic(err)}// handle all requests to your server using the proxy
http.HandleFunc("/",ProxyRequestHandler(proxy))log.Fatal(http.ListenAndServe(":8080",nil))}