Golang gin gonic web框架代理路由到另一个后端

时间:2016-08-16 08:56:29

标签: go go-gin

如何在Gin Gonic web golang框架中将几条路由的代理Web请求反转到另一个后端

有没有办法在Handle功能中直接转发如下所示?

router := gin.New() router.Handle("POST", "/api/v1/endpoint1", ForwardToAnotherBackend)

2 个答案:

答案 0 :(得分:3)

这是我用于从gin框架反向代理端点的特定子集到另一个后端的解决方案:

var n = 0;
var fn = function() {
  return new Promise(function(resolve, reject) {
    console.log("promise " + ++n + " called");
    setTimeout(function(i) {
      resolve(i)
    }, Math.random() * 2500, n)
  })
  // handle requirement 3. here
  .then(function(res) {
    console.log(res);
    return res
  })
}

Promise.all([fn(), fn(), fn()]) // handle requirement 1. here
// handle requirement 2. here
.then(function(data) {
    let [N1, N2, N3] = data;
    console.log(N1, N2, N3);
})

答案 1 :(得分:0)

使用导演时:

req.URL.Scheme = "http"
req.Host, req.URL.Host = "google.com", "google.com"