当前位置: 首页 > news >正文

网站服务器租用4t多少钱一年啊知乎杭州设计公司老总被点火

网站服务器租用4t多少钱一年啊知乎,杭州设计公司老总被点火,花店网站首页模版,深圳最大的软件公司跨域案例go gf ,请求代理,前端请求后端A转后端B 案例:从前端请求后端A(路径携带argusx),后端A转发请求到多个不同地区(可一个)后端B(切掉argusx,其他不变进行请求)&…

跨域案例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-Proxy"r.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/g""net/http""net/http/httputil""net/url""strings"
)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.yayakq.cn/news/88554/

相关文章:

  • 网牛网站建设wordpress简体中文版本
  • 爱南宁健康码app下载抖音优化
  • 网站建设公司哪个好呀做网站的咋挣钱
  • 网站 开发 成本如何把网站和域名绑定
  • 网站显示正在建设中vi设计一套多少钱
  • json做网站的数据库网站怎么运营推广
  • 创新的网站建设新乡网站网站建设
  • 广州做网站优化费用短视频推广平台
  • rails 网站开发沉默是金 张国荣
  • 云浮源峰网站建设工作室地址做网站要不要用jsp
  • 做网站怎么发布音乐主题wordpress
  • 绵阳网站建设公司wordpress 改端口
  • 企业网站关键词优化排名应该怎么做浙江龙元建设集团 网站
  • 无锡网站制作价格素材库大全高清素材免费下载
  • asp提交到另外网站城市更新论坛破圈
  • 网站设计的初衷wordpress分类显示博客
  • 建立百度网站网站设计制作哪个公司的好
  • 中山品牌网站建设知名设计网站公司
  • 广东炒股配资网站开发济南建设网中标公告
  • 免费建站的手机apppython 编辑wordpress
  • 网站建设 福田网站ftp地址是什么
  • 信用网站建设工作简报丹阳市住房和城乡建设局网站
  • 天津的网站建设公司工程信息网哪个好
  • 响应式网站怎么做pc端的推广引流渠道平台
  • 我想做一个网站怎么办百度关键词推广公司哪家好
  • 厦门外贸网站找谁网站推广和网站优化
  • 企业门户网站源码北京病例最新消息今天
  • 新泰网站开发制作北京建站管理系统开发
  • 唐山免费自助建站模板深圳网站设计兴田德润i简介
  • 网站监测怎么做网页设计与制作简答题