before.go 1.9 KB
Newer Older
Y
Your Name 已提交
1
package pluginflow
E
eoLinker API Management 已提交
2 3

import (
黄孟柱 已提交
4 5 6
	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"
E
eoLinker API Management 已提交
7 8 9 10
	"reflect"
	"time"
)

Y
Your Name 已提交
11
//BeforeMatch 执行插件的BeforeMatch函数
E
eoLinker API Management 已提交
12
func BeforeMatch(ctx *common.Context) bool {
Y
Your Name 已提交
13
	requestID := ctx.RequestId()
E
eoLinker API Management 已提交
14
	defer func(ctx *common.Context) {
Y
Your Name 已提交
15
		log.Debug(requestID, " before plugin default: begin")
E
eoLinker API Management 已提交
16 17 18 19 20
		for _, handler := range plugin_manager.GetDefaultPlugins() {
			if handler.PluginObj.BeforeMatch == nil || reflect.ValueOf(handler.PluginObj.BeforeMatch).IsNil() {
				continue
			}
			ctx.SetPlugin(handler.Name)
Y
Your Name 已提交
21
			log.Debug(requestID, " before plugin :", handler.Name, " start")
Y
Your Name 已提交
22 23
			now := time.Now()
			_, err := handler.PluginObj.BeforeMatch.BeforeMatch(ctx)
Y
Your Name 已提交
24 25
			log.Debug(requestID, " before plugin :", handler.Name, " Duration:", time.Since(now))
			log.Debug(requestID, " before plugin :", handler.Name, " end")
E
eoLinker API Management 已提交
26
			if err != nil {
Y
Your Name 已提交
27
				log.Warn(requestID, " before plugin:", handler.Name, " error:", err.Error())
E
eoLinker API Management 已提交
28 29
			}
		}
Y
Your Name 已提交
30
		log.Debug(requestID, " before plugin default: end")
E
eoLinker API Management 已提交
31
	}(ctx)
Y
Your Name 已提交
32
	log.Debug(requestID, " before plugin : begin")
E
eoLinker API Management 已提交
33 34 35 36 37 38 39
	for _, handler := range plugin_manager.GetBeforPlugins() {

		if handler.PluginObj.BeforeMatch == nil || reflect.ValueOf(handler.PluginObj.BeforeMatch).IsNil() {
			continue
		}

		ctx.SetPlugin(handler.Name)
Y
Your Name 已提交
40
		log.Debug(requestID, " before plugin :", handler.Name, " start")
Y
Your Name 已提交
41
		now := time.Now()
E
eoLinker API Management 已提交
42
		flag, err := handler.PluginObj.BeforeMatch.BeforeMatch(ctx)
Y
Your Name 已提交
43 44
		log.Debug(requestID, " before plugin :", handler.Name, " Duration:", time.Since(now))
		log.Debug(requestID, " before plugin :", handler.Name, " end")
E
eoLinker API Management 已提交
45 46

		if err != nil {
Y
Your Name 已提交
47
			log.Warn(requestID, " before plugin:", handler.Name, " error:", err.Error())
E
eoLinker API Management 已提交
48 49 50 51 52 53 54
		}
		if flag == false {
			if handler.IsStop == true {
				return false
			}
		}
	}
Y
Your Name 已提交
55
	log.Debug(requestID, " before plugin : end")
E
eoLinker API Management 已提交
56
	return true
Y
Your Name 已提交
57
}