layer.go 3.1 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
L
Liujian 已提交
44 45 46
	if method == "FOLLOW" {
		method = ctx.ProxyRequest.Method
	}
L
Liujian 已提交
47 48 49 50 51 52 53 54 55
	header:= ctx.ProxyRequest.Headers()
	if b.Encode == "json"{
		header.Set("content-type","application/json; charset=utf-8")
	} else if b.Encode == "xml" {
		header.Set("content-type","application/xml; charset=utf-8")
	}

	r, finalTargetServer, retryTargetServers, err := b.Balance.Send(ctx, b.Protocol, method, path, nil, header, []byte(body), b.TimeOut, b.Retry)

Y
Your Name 已提交
56

Y
Your Name 已提交
57 58
	if err != nil {
		return nil, err
Y
Your Name 已提交
59 60
	}
	backendResponse := &BackendResponse{
Y
Your Name 已提交
61 62
		Method:   strings.ToUpper(method),
		Protocol: b.Protocol,
Y
Your Name 已提交
63
		//Response:           r,
Y
Your Name 已提交
64
		TargetURL:          path,
Y
Your Name 已提交
65 66
		FinalTargetServer:  finalTargetServer,
		RetryTargetServers: retryTargetServers,
Y
Your Name 已提交
67
		Header:             r.Header,
Y
Your Name 已提交
68 69 70
	}

	defer r.Body.Close()
Y
Your Name 已提交
71 72 73 74 75 76 77
	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 已提交
78 79
	if err != nil {
		return backendResponse, nil
Y
Your Name 已提交
80 81
	}

Y
Your Name 已提交
82 83
	rp, e := response.Decode(backendResponse.BodyOrg, b.Decode)
	if e != nil {
Y
Your Name 已提交
84
		backendResponse.Body = nil
Y
Your Name 已提交
85
		return nil, e
Y
Your Name 已提交
86 87 88 89
	}

	b.Filter.Do(rp)

Y
Your Name 已提交
90
	if b.Target != "" {
Y
Your Name 已提交
91 92
		rp.ReTarget(b.Target)
	}
Y
Your Name 已提交
93
	if len(b.Group) > 0 {
Y
Your Name 已提交
94 95
		rp.Group(b.Group)
	}
Y
Your Name 已提交
96

Y
Your Name 已提交
97
	backendResponse.Body = rp.Data
Y
Your Name 已提交
98
	return backendResponse, nil
Y
Your Name 已提交
99 100
}

Y
Your Name 已提交
101
//NewLayer newLayer
Y
Your Name 已提交
102 103 104 105 106 107
func NewLayer(step *config.APIStepConfig) *Layer {
	var b = &Layer{
		BalanceName: step.Balance,
		Balance:     nil,
		HasBalance:  false,
		Protocol:    step.Proto,
Y
Your Name 已提交
108
		Filter:      genFilter(step.BlackList, step.WhiteList, step.Actions),
Y
Your Name 已提交
109
		Method:      strings.ToUpper(step.Method),
Y
Your Name 已提交
110
		Path:        interpreter.GenPath(step.Path),
Y
Your Name 已提交
111 112 113
		Decode:      response.GetDecoder(step.Decode),
		Encode:      step.Encode,
		Target:      step.Target,
Y
Your Name 已提交
114 115
		Group:       nil,
		TimeOut:     time.Duration(step.TimeOut) * time.Millisecond,
Y
Your Name 已提交
116
		Body:        interpreter.Gen(step.Body, step.Encode),
Y
Your Name 已提交
117
		Retry:       step.Retry,
Y
Your Name 已提交
118
	}
Y
Your Name 已提交
119 120
	if step.Group != "" {
		b.Group = strings.Split(step.Group, ".")
Y
Your Name 已提交
121 122 123 124 125 126
	}

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

	return b
}