layer.go 2.7 KB
Newer Older
Y
Your Name 已提交
1 2 3 4
package backend

import (
	"context"
Y
Your Name 已提交
5 6 7 8
	"io/ioutil"
	"strings"
	"time"

Y
Your Name 已提交
9 10 11 12 13 14 15 16 17
	"github.com/eolinker/goku-api-gateway/config"
	"github.com/eolinker/goku-api-gateway/goku-node/common"
	"github.com/eolinker/goku-api-gateway/goku-service/application"
	"github.com/eolinker/goku-api-gateway/goku-service/balance"
	"github.com/eolinker/goku-api-gateway/node/gateway/application/action"
	"github.com/eolinker/goku-api-gateway/node/gateway/application/interpreter"
	"github.com/eolinker/goku-api-gateway/node/gateway/response"
)

Y
Your Name 已提交
18
//Layer layer
Y
Your Name 已提交
19 20
type Layer struct {
	BalanceName string
Y
Your Name 已提交
21 22 23
	Balance     application.IHttpApplication
	HasBalance  bool
	Protocol    string
Y
Your Name 已提交
24 25

	Filter action.Filter
Y
Your Name 已提交
26 27 28
	Method string
	Path   interpreter.Interpreter
	Decode response.DecodeHandle
Y
Your Name 已提交
29

Y
Your Name 已提交
30 31
	Body    interpreter.Interpreter
	Encode  string
Y
Your Name 已提交
32
	Target  string
Y
Your Name 已提交
33
	Group   []string
Y
Your Name 已提交
34 35 36 37
	Retry   int
	TimeOut time.Duration
}

Y
Your Name 已提交
38 39 40 41 42
//Send send
func (b *Layer) Send(deadline context.Context, ctx *common.Context, variables *interpreter.Variables) (*BackendResponse, error) {
	path := b.Path.Execution(variables)
	body := b.Body.Execution(variables)
	method := b.Method
Y
Your Name 已提交
43

Y
Your Name 已提交
44
	r, finalTargetServer, retryTargetServers, err := b.Balance.Send(b.Protocol, method, path, ctx.ProxyRequest.Querys(), ctx.ProxyRequest.Headers(), []byte(body), b.TimeOut, b.Retry)
Y
Your Name 已提交
45

Y
Your Name 已提交
46 47
	if err != nil {
		return nil, err
Y
Your Name 已提交
48 49
	}
	backendResponse := &BackendResponse{
Y
Your Name 已提交
50 51
		Method:   strings.ToUpper(method),
		Protocol: b.Protocol,
Y
Your Name 已提交
52
		//Response:           r,
Y
Your Name 已提交
53
		TargetURL:          path,
Y
Your Name 已提交
54 55
		FinalTargetServer:  finalTargetServer,
		RetryTargetServers: retryTargetServers,
Y
Your Name 已提交
56
		Header:             r.Header,
Y
Your Name 已提交
57 58 59 60
	}

	defer r.Body.Close()
	backendResponse.BodyOrg, err = ioutil.ReadAll(r.Body)
Y
Your Name 已提交
61 62
	if err != nil {
		return backendResponse, nil
Y
Your Name 已提交
63 64
	}

Y
Your Name 已提交
65 66
	rp, e := response.Decode(backendResponse.BodyOrg, b.Decode)
	if e != nil {
Y
Your Name 已提交
67
		backendResponse.Body = nil
Y
Your Name 已提交
68
		return nil, e
Y
Your Name 已提交
69 70 71 72
	}

	b.Filter.Do(rp)

Y
Your Name 已提交
73
	if b.Target != "" {
Y
Your Name 已提交
74 75
		rp.ReTarget(b.Target)
	}
Y
Your Name 已提交
76
	if len(b.Group) > 0 {
Y
Your Name 已提交
77 78 79
		rp.Group(b.Group)
	}
	backendResponse.Body = rp.Data
Y
Your Name 已提交
80
	return backendResponse, nil
Y
Your Name 已提交
81 82
}

Y
Your Name 已提交
83
//NewLayer newLayer
Y
Your Name 已提交
84 85 86 87 88 89
func NewLayer(step *config.APIStepConfig) *Layer {
	var b = &Layer{
		BalanceName: step.Balance,
		Balance:     nil,
		HasBalance:  false,
		Protocol:    step.Proto,
Y
Your Name 已提交
90
		Filter:      genFilter(step.BlackList, step.WhiteList, step.Actions),
Y
Your Name 已提交
91
		Method:      strings.ToUpper(step.Method),
Y
Your Name 已提交
92
		Path:        interpreter.GenPath(step.Path),
Y
Your Name 已提交
93 94 95
		Decode:      response.GetDecoder(step.Decode),
		Encode:      step.Encode,
		Target:      step.Target,
Y
Your Name 已提交
96 97 98 99
		Group:       nil,
		TimeOut:     time.Duration(step.TimeOut) * time.Millisecond,
		Body:        interpreter.Gen(step.Body),
		Retry:       step.Retry,
Y
Your Name 已提交
100
	}
Y
Your Name 已提交
101 102
	if step.Group != "" {
		b.Group = strings.Split(step.Group, ".")
Y
Your Name 已提交
103 104 105 106 107 108
	}

	b.Balance, b.HasBalance = balance.GetByName(b.BalanceName)

	return b
}