proxy.go 2.9 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6
package application

import (
	"fmt"
	"strings"

Y
Your Name 已提交
7 8
	"github.com/eolinker/goku-api-gateway/goku-service/application"

Y
Your Name 已提交
9 10 11 12 13 14 15 16 17 18
	"github.com/eolinker/goku-api-gateway/config"
	log "github.com/eolinker/goku-api-gateway/goku-log"
	"github.com/eolinker/goku-api-gateway/goku-node/common"
	"github.com/eolinker/goku-api-gateway/node/gateway/application/backend"
	"github.com/eolinker/goku-api-gateway/node/gateway/application/interpreter"

	"github.com/eolinker/goku-api-gateway/node/gateway/response"
	access_field "github.com/eolinker/goku-api-gateway/server/access-field"
)

Y
Your Name 已提交
19
//DefaultApplication default application
Y
Your Name 已提交
20 21 22 23 24 25 26
type DefaultApplication struct {
	output        response.Encoder
	backend       *backend.Proxy
	static        *staticeResponse
	balanceTarget string
}

Y
Your Name 已提交
27
//NewDefaultApplication create new default application
Y
Your Name 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
func NewDefaultApplication(apiContent *config.APIContent, target string) *DefaultApplication {

	app := &DefaultApplication{
		backend:       nil,
		static:        nil,
		balanceTarget: target,
		output:        response.GetEncoder(apiContent.OutPutEncoder),
	}
	if len(apiContent.Steps) == 1 {
		step := apiContent.Steps[0]
		app.backend = backend.NewProxyBackendTarget(step, apiContent.RequestURL, target)
	}
	if apiContent.StaticResponse != "" {
		staticResponseStrategy := config.Parse(apiContent.StaticResponseStrategy)
		app.static = newStaticeResponse(apiContent.StaticResponse, staticResponseStrategy)
	}

	return app
}

Y
Your Name 已提交
48
//Execute execute
Y
Your Name 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61
func (app *DefaultApplication) Execute(ctx *common.Context) {

	ctx.LogFields[access_field.Balance] = app.balanceTarget

	if app.backend != nil {
		orgBody, _ := ctx.ProxyRequest.RawBody()

		variables := interpreter.NewVariables(orgBody, nil, ctx.ProxyRequest.Headers(), ctx.ProxyRequest.Cookies(), ctx.RestfulParam, ctx.ProxyRequest.Querys(), 1)

		r, err := app.backend.Send(ctx, variables)
		if r != nil {

			ctx.ProxyRequest.Method = r.Method
Y
Your Name 已提交
62
			ctx.ProxyRequest.SetTargetURL(r.TargetURL)
Y
Your Name 已提交
63 64 65 66 67 68

			ctx.SetRetryTargetServers(strings.Join(r.RetryTargetServers, ","))
			ctx.SetFinalTargetServer(r.FinalTargetServer)

			ctx.LogFields[access_field.FinallyServer] = ctx.FinalTargetServer()
			ctx.LogFields[access_field.Retry] = ctx.RetryTargetServers()
Y
Your Name 已提交
69
			ctx.LogFields[access_field.Proxy] = fmt.Sprintf("\"%s %s %s\"", r.Method, application.URLPath(r.TargetURL, ctx.ProxyRequest.Querys()), r.Protocol)
Y
Your Name 已提交
70 71 72

		}
		if err != nil {
Y
Your Name 已提交
73 74
			ctx.SetStatus(504, "504")
			ctx.SetBody([]byte("[ERROR]Fail to get response after proxy!"))
Y
Your Name 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
			log.Warn(err)
			return
		}

		ctx.LogFields[access_field.ProxyStatusCode] = r.StatusCode

		body, err := app.output.Encode(r.Body, r.BodyOrg)
		if err != nil {
			body = r.BodyOrg
		}
		ctx.SetProxyResponseHandler(common.NewResponseReader(r.Header, r.StatusCode, r.Status, body))

		return

	}
	if app.static != nil {
		app.static.Do(ctx)
		return
	}

	ctx.SetStatus(504, "504")
	ctx.SetBody([]byte("[ERROR]Fail to get response after proxy!"))

}