reverse_proxy_controller.go
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package mobile_client
import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
)
//代理请求转发
type ReverseProxyController struct {
baseController
}
func (controller *ReverseProxyController) SuplusSaleApp() {
// 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 {
panic(err)
}
controller.Ctx.Request.URL.Path = strings.Replace(controller.Ctx.Request.URL.Path, "/suplus-sale-app", "", 1)
newProxy := httputil.NewSingleHostReverseProxy(target)
directorFunc := newProxy.Director
newDirectorFunc := func(r *http.Request) {
directorFunc(r)
r.Host = target.Host
}
newProxy.Director = newDirectorFunc
newProxy.ServeHTTP(controller.Ctx.ResponseWriter, controller.Ctx.Request)
}