佛山优化网站排名收费,北京公司网站制作,如何推广自己的微信公众号,公司注册网上签字流程跨域案例go gf #xff0c;请求代理#xff0c;前端请求后端A转后端B
案例#xff1a;从前端请求后端A#xff08;路径携带argusx#xff09;#xff0c;后端A转发请求到多个不同地区#xff08;可一个#xff09;后端B(切掉argusx#xff0c;其他不变进行请求)…跨域案例go gf 请求代理前端请求后端A转后端B
案例从前端请求后端A路径携带argusx后端A转发请求到多个不同地区可一个后端B(切掉argusx其他不变进行请求)由请求头x-proxy指定请求哪个服务端
方案一handler形式处理
func InitRouter() {s : g.Server()// 分组路由注册方式app.Api s.Group(/argusx, func(group *ghttp.RouterGroup) {// 跨域中间件service.Middleware.InitGroup(group)ReverseProxy(group, /xxx/)})
}// InitGroup 注册中间件
func (s *middlewareService) InitGroup(group *ghttp.RouterGroup) {group.Middleware(// 跨域处理s.CORS,//s.Auth,)
}
// 使用默认跨域处理
func (s *middlewareService) CORS(r *ghttp.Request) {//r.Response.CORSDefault() //若是请求头没有新的则直接用default否则用下面三行代码options : r.Response.DefaultCORSOptions()options.AllowHeaders options.AllowHeaders ,X-Proxyr.Response.CORS(options)r.Middleware.Next()
}
func ReverseProxy(pg *ghttp.RouterGroup, path string) {op : func(r *ghttp.Request) {// 根据code拿到地址可用自己的方式获取参数argusvoiceCode : r.GetHeader(x-proxy)g.Log().Infof([argusvoice] argusvoiceCode%s, argusvoiceCode)if argusvoiceCode ! {argusvoiceApi : service.GetArgusVoiceUrlByCode(argusvoiceCode)g.Log().Infof([argusvoice] argusvoiceApi%s, argusvoiceApi)remote, err : url.Parse(argusvoiceApi)if err ! nil {fmt.Println(err.Error())g.Log().Errorf([argusvoice] url parse error, argusvoiceApi%s, error%v, argusvoiceApi, err)}reverseProxy : proxy.GoReverseProxy(proxy.RProxy{Remote: remote,})if err ! nil {fmt.Println(err.Error())r.ExitAll()return}reverseProxy.ServeHTTP(r.Response.Writer, r.Request)r.ExitAll()} else {r.Middleware.Next()}}pg.Bind([]ghttp.GroupItem{{ALL, path *, op}})
}
方案二中间件的形式代理 对所有请求都拦截包括options这样需要自己处理options请求options请求是为了协商请求头所以需要返回成功以及必要信息方便后期请求携带。 请求允许的加上x-proxy注意option请求是拿不到具体的值 坏处无法使用框架自带的options处理
s.BindMiddlewareDefault(func(r *ghttp.Request) {// 根据code拿到地址accessControlHeaders : r.Header.Get(Access-Control-Request-Headers)isProxy : strings.Contains(accessControlHeaders, x-proxy)argusvoiceCode : r.GetHeader(x-proxy)r.Header.Set(Access-Control-Allow-Origin, *)g.Log().Infof([argusvoice] argusvoiceCode%s, argusvoiceCode)if isProxy || (argusvoiceCode ! ) {argusvoiceApi : service.GetArgusVoiceUrlByCode(argusvoiceCode)if r.Request.Method OPTIONS {r.Response.Status 200// 以下头部请参照自己正常请求的头部r.Response.Header().Set(Access-Control-Allow-Origin, r.Header.Get(Origin)) //此处不能写作*r.Response.Header().Set(Access-Control-Allow-Credentials, true)r.Response.Header().Set(Access-Control-Allow-Headers, Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, x-token, x-requested-with,Accept,Origin,Referer,User-Agent,x-proxy)r.Response.Header().Set(Access-Control-Allow-Methods, POST, GET, OPTIONS, PUT, PATCH, DELETE)r.Response.Header().Set(Access-Control-Expose-Headers, Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type)return}g.Log().Infof([argusvoice] argusvoiceApi%s, argusvoiceApi)remote, err : url.Parse(argusvoiceApi)if err ! nil {fmt.Println(err.Error())g.Log().Errorf([argusvoice] url parse error, argusvoiceApi%s, error%v, argusvoiceApi, err)}reverseProxy : proxy.GoReverseProxy(proxy.RProxy{Remote: remote,})if err ! nil {fmt.Println(err.Error())r.ExitAll()return}reverseProxy.ServeHTTP(r.Response.Writer, r.Request)r.ExitAll()} else {r.Middleware.Next()}
})
proxy文件此处代码大部分抄自https://github.com/hezhizheng/go-reverse-proxy/blob/master/handle.go 该项目使用案例https://hzz.cool/blog/implementation-of-simple-http-and-https-reverse-proxy-by-golang
package proxyimport (github.com/gogf/gf/frame/gnet/httpnet/http/httputilnet/urlstrings
)type RProxy struct {Remote *url.URL
}func GoReverseProxy(this *RProxy) *httputil.ReverseProxy {remote : this.Remoteproxy : httputil.NewSingleHostReverseProxy(remote)proxy.Director func(request *http.Request) {Path : targetQuery : remote.RawQueryrequest.URL.Scheme remote.Schemerequest.URL.Host remote.Hostrequest.Host remote.HostPath, request.URL.RawPath joinURLPath(remote, request.URL)Paths : strings.Split(Path, /argusx)request.URL.Path Paths[1]g.Log().Infof([argusvoice] request.Body%v, request.Body)if targetQuery || request.URL.RawQuery {request.URL.RawQuery targetQuery request.URL.RawQuery} else {request.URL.RawQuery targetQuery request.URL.RawQuery}g.Log().Infof([argusvoice] request.URL.Path%s, request.URL.RawQuery%s, request.URL.Path, request.URL.RawQuery)}proxy.ModifyResponse func(response *http.Response) error {response.Header.Del(Access-Control-Allow-Origin)response.Header.Del(Access-Control-Allow-Credentials)response.Header.Del(Access-Control-Allow-Headers)response.Header.Del(Access-Control-Allow-Methods)return nil}return proxy
}// go sdk 源码
func joinURLPath(a, b *url.URL) (path, rawpath string) {if a.RawPath b.RawPath {return singleJoiningSlash(a.Path, b.Path), }// Same as singleJoiningSlash, but uses EscapedPath to determine// whether a slash should be addedapath : a.EscapedPath()bpath : b.EscapedPath()aslash : strings.HasSuffix(apath, /)bslash : strings.HasPrefix(bpath, /)switch {case aslash bslash:return a.Path b.Path[1:], apath bpath[1:]case !aslash !bslash:return a.Path / b.Path, apath / bpath}return a.Path b.Path, apath bpath
}// go sdk 源码
func singleJoiningSlash(a, b string) string {aslash : strings.HasSuffix(a, /)bslash : strings.HasPrefix(b, /)switch {case aslash bslash:return a b[1:]case !aslash !bslash:return a / b}return a b
} 文章转载自: http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.nppml.cn.gov.cn.nppml.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.yrnll.cn.gov.cn.yrnll.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.xqxlb.cn.gov.cn.xqxlb.cn http://www.morning.qjghx.cn.gov.cn.qjghx.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.brqjs.cn.gov.cn.brqjs.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.nqbcj.cn.gov.cn.nqbcj.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.rlhh.cn.gov.cn.rlhh.cn http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.hgkbj.cn.gov.cn.hgkbj.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn http://www.morning.sgrwd.cn.gov.cn.sgrwd.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.gydth.cn.gov.cn.gydth.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.ndnhf.cn.gov.cn.ndnhf.cn http://www.morning.hhxpl.cn.gov.cn.hhxpl.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.pdwzr.cn.gov.cn.pdwzr.cn http://www.morning.tdqhs.cn.gov.cn.tdqhs.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn