|
|
package mobile_client
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"net/http"
|
|
|
"net/http/httputil"
|
|
|
"net/url"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/beego/beego/v2/client/httplib"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers"
|
|
|
)
|
|
|
|
|
|
//代理请求转发
|
|
|
|
|
|
type ReverseProxyController struct {
|
|
|
controllers.BaseController
|
|
|
baseController
|
|
|
}
|
|
|
|
|
|
func (controller *ReverseProxyController) SuplusSaleApp() {
|
|
|
//routePrefix := "/suplus-sale-app"
|
|
|
proxyHostUrl := constant.SUPLUS_SALE_APP + "/v1/platform/district"
|
|
|
req := httplib.Post(proxyHostUrl)
|
|
|
req.Header("Content-Type", "application/json")
|
|
|
req = req.Body(controller.Ctx.Input.RequestBody)
|
|
|
fmt.Println(string(controller.Ctx.Input.RequestBody))
|
|
|
// req.JSONBody() b.req.Header.Set(, "application/json")
|
|
|
respData, err := req.Bytes()
|
|
|
// proxyHostUrl := constant.SUPLUS_SALE_APP + "/v1/platform/district"
|
|
|
// req := httplib.Post(proxyHostUrl)
|
|
|
// req.Header("Content-Type", "application/json")
|
|
|
// req = req.Body(controller.Ctx.Input.RequestBody)
|
|
|
// respData, err := req.Bytes()
|
|
|
// if err != nil {
|
|
|
// controller.Response(nil, err)
|
|
|
// return
|
|
|
// }
|
|
|
// controller.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
|
|
|
// controller.Ctx.Output.Body(respData)
|
|
|
target, err := url.Parse(constant.SUPLUS_SALE_APP)
|
|
|
if err != nil {
|
|
|
controller.Response(nil, err)
|
|
|
return
|
|
|
panic(err)
|
|
|
}
|
|
|
controller.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
|
|
|
controller.Ctx.Output.Body(respData)
|
|
|
controller.Ctx.Request.URL.Path = strings.Replace(controller.Ctx.Request.URL.Path, "/suplus-sale-app", "", 1)
|
|
|
targetQuery := target.RawQuery
|
|
|
directorFunc := func(req *http.Request) {
|
|
|
req.Host = target.Host
|
|
|
req.URL.Scheme = target.Scheme
|
|
|
req.URL.Host = target.Host
|
|
|
req.URL.Path, req.URL.RawPath = joinURLPath(target, controller.Ctx.Request.URL)
|
|
|
if targetQuery == "" || req.URL.RawQuery == "" {
|
|
|
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
|
|
} else {
|
|
|
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
|
|
|
}
|
|
|
if _, ok := req.Header["User-Agent"]; !ok {
|
|
|
// explicitly disable User-Agent so it's not set to default value
|
|
|
req.Header.Set("User-Agent", "")
|
|
|
}
|
|
|
}
|
|
|
newProxy := &httputil.ReverseProxy{Director: directorFunc}
|
|
|
newProxy.ServeHTTP(controller.Ctx.ResponseWriter, controller.Ctx.Request)
|
|
|
}
|
|
|
|
|
|
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 added
|
|
|
apath := 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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
} |
...
|
...
|
|