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