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

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

	"strings"
)

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

Y
Your Name 已提交
19 20 21
	app, has := balance_manager.Get(apiInfo.Target)
	if !has {
		err := fmt.Errorf("get balance error:%s", apiInfo.Target)
Y
Your Name 已提交
22
		return nil, err
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
	if err != nil {
Y
Your Name 已提交
32
		return nil, err
E
eoLinker API Management 已提交
33
	}
Y
Your Name 已提交
34
	return response, nil
E
eoLinker API Management 已提交
35 36

}