正在显示
1 个修改的文件
包含
72 行增加
和
16 行删除
1 | package mobile_client | 1 | package mobile_client |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "fmt" | 4 | + "bytes" |
5 | + "io/ioutil" | ||
6 | + "net/http" | ||
7 | + "net/http/httputil" | ||
8 | + "net/url" | ||
9 | + "strings" | ||
5 | 10 | ||
6 | - "github.com/beego/beego/v2/client/httplib" | ||
7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant" | 11 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant" |
8 | - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers" | ||
9 | ) | 12 | ) |
10 | 13 | ||
11 | //代理请求转发 | 14 | //代理请求转发 |
12 | 15 | ||
13 | type ReverseProxyController struct { | 16 | type ReverseProxyController struct { |
14 | - controllers.BaseController | 17 | + baseController |
15 | } | 18 | } |
16 | 19 | ||
17 | func (controller *ReverseProxyController) SuplusSaleApp() { | 20 | func (controller *ReverseProxyController) SuplusSaleApp() { |
18 | - //routePrefix := "/suplus-sale-app" | ||
19 | - proxyHostUrl := constant.SUPLUS_SALE_APP + "/v1/platform/district" | ||
20 | - req := httplib.Post(proxyHostUrl) | ||
21 | - req.Header("Content-Type", "application/json") | ||
22 | - req = req.Body(controller.Ctx.Input.RequestBody) | ||
23 | - fmt.Println(string(controller.Ctx.Input.RequestBody)) | ||
24 | - // req.JSONBody() b.req.Header.Set(, "application/json") | ||
25 | - respData, err := req.Bytes() | 21 | + // proxyHostUrl := constant.SUPLUS_SALE_APP + "/v1/platform/district" |
22 | + // req := httplib.Post(proxyHostUrl) | ||
23 | + // req.Header("Content-Type", "application/json") | ||
24 | + // req = req.Body(controller.Ctx.Input.RequestBody) | ||
25 | + // respData, err := req.Bytes() | ||
26 | + // if err != nil { | ||
27 | + // controller.Response(nil, err) | ||
28 | + // return | ||
29 | + // } | ||
30 | + // controller.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8") | ||
31 | + // controller.Ctx.Output.Body(respData) | ||
32 | + target, err := url.Parse(constant.SUPLUS_SALE_APP) | ||
26 | if err != nil { | 33 | if err != nil { |
27 | - controller.Response(nil, err) | ||
28 | - return | 34 | + panic(err) |
29 | } | 35 | } |
30 | - controller.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8") | ||
31 | - controller.Ctx.Output.Body(respData) | 36 | + controller.Ctx.Request.Body = ioutil.NopCloser(bytes.NewReader(controller.Ctx.Input.RequestBody)) |
37 | + controller.Ctx.Request.URL.Path = strings.Replace(controller.Ctx.Request.URL.Path, "/suplus-sale-app", "", 1) | ||
38 | + targetQuery := target.RawQuery | ||
39 | + director := func(req *http.Request) { | ||
40 | + req.Host = target.Host | ||
41 | + req.URL.Scheme = target.Scheme | ||
42 | + req.URL.Host = target.Host | ||
43 | + req.URL.Path, req.URL.RawPath = joinURLPath(target, controller.Ctx.Request.URL) | ||
44 | + if targetQuery == "" || req.URL.RawQuery == "" { | ||
45 | + req.URL.RawQuery = targetQuery + req.URL.RawQuery | ||
46 | + } else { | ||
47 | + req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery | ||
48 | + } | ||
49 | + if _, ok := req.Header["User-Agent"]; !ok { | ||
50 | + // explicitly disable User-Agent so it's not set to default value | ||
51 | + req.Header.Set("User-Agent", "") | ||
52 | + } | ||
53 | + } | ||
54 | + newProxy := &httputil.ReverseProxy{Director: director} | ||
55 | + newProxy.ServeHTTP(controller.Ctx.ResponseWriter, controller.Ctx.Request) | ||
56 | +} | ||
57 | + | ||
58 | +func joinURLPath(a, b *url.URL) (path, rawpath string) { | ||
59 | + if a.RawPath == "" && b.RawPath == "" { | ||
60 | + return singleJoiningSlash(a.Path, b.Path), "" | ||
61 | + } | ||
62 | + // Same as singleJoiningSlash, but uses EscapedPath to determine | ||
63 | + // whether a slash should be added | ||
64 | + apath := a.EscapedPath() | ||
65 | + bpath := b.EscapedPath() | ||
66 | + | ||
67 | + aslash := strings.HasSuffix(apath, "/") | ||
68 | + bslash := strings.HasPrefix(bpath, "/") | ||
32 | 69 | ||
70 | + switch { | ||
71 | + case aslash && bslash: | ||
72 | + return a.Path + b.Path[1:], apath + bpath[1:] | ||
73 | + case !aslash && !bslash: | ||
74 | + return a.Path + "/" + b.Path, apath + "/" + bpath | ||
75 | + } | ||
76 | + return a.Path + b.Path, apath + bpath | ||
77 | +} | ||
78 | + | ||
79 | +func singleJoiningSlash(a, b string) string { | ||
80 | + aslash := strings.HasSuffix(a, "/") | ||
81 | + bslash := strings.HasPrefix(b, "/") | ||
82 | + switch { | ||
83 | + case aslash && bslash: | ||
84 | + return a + b[1:] | ||
85 | + case !aslash && !bslash: | ||
86 | + return a + "/" + b | ||
87 | + } | ||
88 | + return a + b | ||
33 | } | 89 | } |
-
请 注册 或 登录 后发表评论