proxy.go 2.0 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3
package plugin_flow

import (
黄孟柱 已提交
4 5 6 7
	log "github.com/eolinker/goku-api-gateway/goku-log"
	"github.com/eolinker/goku-api-gateway/goku-node/common"
	plugin_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/plugin-manager"
	entity "github.com/eolinker/goku-api-gateway/server/entity/node-entity"
E
eoLinker API Management 已提交
8 9 10 11 12 13 14 15 16

	"reflect"
	"time"
)

// 执行插件的Proxy函数
func ProxyFunc(ctx *common.Context, handleFunc []*entity.PluginHandlerExce) (bool, int) {
	requestId := ctx.RequestId()
	defer func(ctx *common.Context) {
Y
Your Name 已提交
17
		log.Debug(requestId, " Proxy plugin default: begin")
E
eoLinker API Management 已提交
18 19 20 21 22
		for _, handler := range plugin_manager.GetDefaultPlugins() {
			if handler.PluginObj.Proxy == nil || reflect.ValueOf(handler.PluginObj.Proxy).IsNil() {
				continue
			}
			ctx.SetPlugin(handler.Name)
Y
Your Name 已提交
23 24 25 26 27
			log.Debug(requestId, " Proxy plugin :", handler.Name, " start")
			now := time.Now()
			_, err := handler.PluginObj.Proxy.Proxy(ctx)
			log.Debug(requestId, " Proxy plugin :", handler.Name, " Duration:", time.Since(now))
			log.Debug(requestId, " Proxy plugin :", handler.Name, " end")
E
eoLinker API Management 已提交
28 29

			if err != nil {
Y
Your Name 已提交
30
				log.Warn(requestId, " Proxy plugin:", handler.Name, " error:", err.Error())
E
eoLinker API Management 已提交
31 32
			}
		}
Y
Your Name 已提交
33
		log.Debug(requestId, " Proxy plugin default: begin")
E
eoLinker API Management 已提交
34 35
	}(ctx)
	lastIndex := 0
Y
Your Name 已提交
36
	log.Debug(requestId, " Proxy plugin : begin")
E
eoLinker API Management 已提交
37 38 39 40 41 42 43
	for index, handler := range handleFunc {
		lastIndex = index
		if handler.PluginObj.Proxy == nil || reflect.ValueOf(handler.PluginObj.Proxy).IsNil() {
			continue
		}

		ctx.SetPlugin(handler.Name)
Y
Your Name 已提交
44 45
		log.Debug(requestId, " Proxy plugin :", handler.Name, " start")
		now := time.Now()
E
eoLinker API Management 已提交
46
		flag, err := handler.PluginObj.Proxy.Proxy(ctx)
Y
Your Name 已提交
47 48
		log.Debug(requestId, " Proxy plugin :", handler.Name, " Duration:", time.Since(now))
		log.Debug(requestId, " Proxy plugin :", handler.Name, " end")
E
eoLinker API Management 已提交
49 50

		if err != nil {
Y
Your Name 已提交
51
			log.Warn(requestId, " Proxy plugin :", handler.Name, " error: ", err.Error())
E
eoLinker API Management 已提交
52 53 54 55 56 57
		}
		if flag == false && handler.IsStop == true {

			return false, lastIndex
		}
	}
Y
Your Name 已提交
58
	log.Debug(requestId, " Proxy plugin : end")
E
eoLinker API Management 已提交
59 60
	return true, lastIndex
}