diff --git a/src/handler/proxyhandler.go b/src/handler/proxyhandler.go index a50b008ef3a2af9e3f016a3bf621242aaea84e7a..bffd39c0885c1e5317948770070e6ebe1ac50ae4 100644 --- a/src/handler/proxyhandler.go +++ b/src/handler/proxyhandler.go @@ -77,6 +77,15 @@ func Proxy(c *gin.Context) { //如果是静态文件 if CheckStaticFile(c.Request.URL.Path) { host := getHost(proxyData.ReqUrl) + //var prefix string + //if apiId == "" { + // prefix= fmt.Sprintf("%s/%s/service/%s/%s/", config.Prefix, config.MeshId,applyId,apiId) + //}else { + // prefix= fmt.Sprintf("%s/%s/service/%s/", config.Prefix, config.MeshId,applyId) + //} + // + //staticPath:= strings.ReplaceAll(c.Request.URL.Path,prefix,"") + fwd, _ := forward.New(forward.PassHostHeader(false)) c.Request.URL = testutils.ParseURI(fmt.Sprintf("%s%s", host, c.Request.URL.Path)) c.Request.RequestURI = getRequestURI(c.Request) @@ -86,7 +95,7 @@ func Proxy(c *gin.Context) { roundTripper := http.DefaultTransport.(*http.Transport) //roundTripper.ForceAttemptHTTP2 = false roundTripper.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} - f, err := forward.New(forward.PassHostHeader(false), forward.RoundTripper(roundTripper), forward.ResponseModifier(func(resp *http.Response) error { + forwarder, err := forward.New(forward.PassHostHeader(false), forward.RoundTripper(roundTripper), forward.ResponseModifier(func(resp *http.Response) error { respbody, err1 := ioutil.ReadAll(resp.Body) if err1 != nil { log.Println(err1) @@ -148,15 +157,25 @@ func Proxy(c *gin.Context) { return nil })) if err != nil { - + c.JSON(500, errors.New("请求失败")) + return + } + reqURL := testutils.ParseURI(proxyData.ReqUrl) + if reqURL.RawQuery == "" { + reqURL.RawQuery = c.Request.URL.RawQuery + } else { + if c.Request.URL.RawQuery != "" { + reqURL.RawQuery += "&" + c.Request.URL.RawQuery + } } - c.Request.URL = testutils.ParseURI(proxyData.ReqUrl) + + c.Request.URL = reqURL fmt.Println("c.Request.URL-------", c.Request.URL) - c.Request.RequestURI = getRequestURI(c.Request) + c.Request.RequestURI = reqURL.RequestURI() fmt.Println("c.Request-------", c.Request) c.Request.Host = getHost(proxyData.ReqUrl) fmt.Println("proxyData.RealUrl-------", proxyData.ReqUrl) - f.ServeHTTP(c.Writer, c.Request) + forwarder.ServeHTTP(c.Writer, c.Request) } } @@ -169,26 +188,12 @@ func CheckStaticFile(path string) bool { return false } -func getProxyURL(proxyData model.ProxyData, req *http.Request) string { - realPath := proxyData.ReqUrl - rawQuery := req.URL.RawQuery - var result = "" - if rawQuery == "" { - if proxyData.DataServiceType1 == 6 { - result = realPath + "?f=json" - } else { - result = realPath - } - } else { - result = realPath + "?" + rawQuery - if proxyData.DataServiceType1 == 6 && !strings.Contains(rawQuery, "f=json") { - result = result + "&f=json" - } - } - return result -} - func getRequestURI(req *http.Request) string { + + //requestURI, _ := url.ParseRequestURI(req.RequestURI) + // + //requestURI.RawQuery + path := req.URL.Path rawQuery := req.URL.RawQuery var result = "" diff --git a/src/main.go b/src/main.go index f683fa2de6b8c96411dae797a95f5ab7aea42879..e178e307bc3e6df3af8727867b871907b838e5ad 100644 --- a/src/main.go +++ b/src/main.go @@ -18,7 +18,7 @@ import ( var ( argPort = pflag.Int("port", 8011, "") argPrefix = pflag.String("prefix", "/bgmesh/fiddler", "") - meshId = pflag.String("meshId", "133", "") + meshId = pflag.String("meshId", "461", "") redisUrl = pflag.String("redisUrl", "redis://apaas-redis:6379", "") redisTag = pflag.String("redisTag", "apaas-mesh-proxy", "") confPath = pflag.String("confPath", "/app/config/proxy.json", "") @@ -33,6 +33,7 @@ func main() { server() } +//打包 func server() error { config.Prefix = *argPrefix handler := router.Load(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true))