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

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

Y
Your Name 已提交
10 11 12 13 14 15 16 17 18
	"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 已提交
19
//Layer layer
Y
Your Name 已提交
20 21
type Layer struct {
	BalanceName string
Y
Your Name 已提交
22 23 24
	Balance     application.IHttpApplication
	HasBalance  bool
	Protocol    string
Y
Your Name 已提交
25 26

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

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

Y
Your Name 已提交
39 40 41 42 43
//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 已提交
44

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

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

	defer r.Body.Close()
Y
Your Name 已提交
61 62 63 64 65 66 67
	bd := r.Body
	if r.Header.Get("Content-Encoding") == "gzip" {
		bd, _ = gzip.NewReader(r.Body)
		r.Header.Del("Content-Encoding")
	}

	backendResponse.BodyOrg, err = ioutil.ReadAll(bd)
Y
Your Name 已提交
68 69
	if err != nil {
		return backendResponse, nil
Y
Your Name 已提交
70 71
	}

Y
Your Name 已提交
72 73
	rp, e := response.Decode(backendResponse.BodyOrg, b.Decode)
	if e != nil {
Y
Your Name 已提交
74
		backendResponse.Body = nil
Y
Your Name 已提交
75
		return nil, e
Y
Your Name 已提交
76 77 78 79
	}

	b.Filter.Do(rp)

Y
Your Name 已提交
80
	if b.Target != "" {
Y
Your Name 已提交
81 82
		rp.ReTarget(b.Target)
	}
Y
Your Name 已提交
83
	if len(b.Group) > 0 {
Y
Your Name 已提交
84 85
		rp.Group(b.Group)
	}
Y
Your Name 已提交
86

Y
Your Name 已提交
87
	backendResponse.Body = rp.Data
Y
Your Name 已提交
88
	return backendResponse, nil
Y
Your Name 已提交
89 90
}

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

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

	return b
}