request_mapping.go 1.0 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package goku_node

import (
	"fmt"
	"net/http"
	"time"

黄孟柱 已提交
8
	"github.com/eolinker/goku-api-gateway/goku-node/common"
E
eoLinker API Management 已提交
9

黄孟柱 已提交
10 11
	balance_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/balance-manager"
	entity "github.com/eolinker/goku-api-gateway/server/entity/node-entity"
E
eoLinker API Management 已提交
12 13 14 15 16

	"strings"
)

// 创建转发请求
Y
Your Name 已提交
17
func CreateRequest(ctx *common.Context, apiInfo *entity.ApiExtend, timeout, retry int) (error, *http.Response) {
E
eoLinker API Management 已提交
18

Y
Your Name 已提交
19 20 21 22
	app, has := balance_manager.Get(apiInfo.Target)
	if !has {
		err := fmt.Errorf("get balance error:%s", apiInfo.Target)
		return err, nil
E
eoLinker API Management 已提交
23 24 25
	}
	rawbody, _ := ctx.ProxyRequest.RawBody()

Y
Your Name 已提交
26
	response, finalTargetServer, retryTargetServers, err := app.Send(apiInfo.Protocol, ctx.ProxyRequest.Method, ctx.ProxyRequest.TargetURL(), ctx.ProxyRequest.Querys(), ctx.ProxyRequest.Headers(), rawbody, time.Duration(timeout)*time.Millisecond, retry)
E
eoLinker API Management 已提交
27 28 29 30

	ctx.SetRetryTargetServers(strings.Join(retryTargetServers, ","))
	ctx.SetFinalTargetServer(finalTargetServer)

Y
Your Name 已提交
31 32
	if err != nil {
		return err, nil
E
eoLinker API Management 已提交
33
	}
Y
Your Name 已提交
34
	return nil, response
E
eoLinker API Management 已提交
35 36

}