executor.go 3.2 KB
Newer Older
Y
Your Name 已提交
1 2 3
package plugin_executor

import (
Y
Your Name 已提交
4 5
	"time"

Y
Your Name 已提交
6 7 8
	"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"
Y
Your Name 已提交
9
	goku_plugin "github.com/eolinker/goku-plugin"
Y
Your Name 已提交
10 11
)

Y
Your Name 已提交
12
//Executor executor
Y
Your Name 已提交
13
type Executor interface {
Y
Your Name 已提交
14 15 16
	Execute(ctx *common.Context) (isContinue bool, e error)
	IsStop() bool
	IsAuth() bool
Y
Your Name 已提交
17 18
}
type executorInfo struct {
Y
Your Name 已提交
19
	Name   string
Y
Your Name 已提交
20
	isStop bool
Y
Your Name 已提交
21
	isAuth bool
Y
Your Name 已提交
22 23
}

Y
Your Name 已提交
24 25 26
func (ex *executorInfo) IsStop() bool {
	return ex.isStop
}
Y
Your Name 已提交
27

Y
Your Name 已提交
28 29
func (ex *executorInfo) IsAuth() bool {
	return ex.isAuth
Y
Your Name 已提交
30 31 32 33
}

func genExecutor(cfg *config.PluginConfig) executorInfo {
	return executorInfo{
Y
Your Name 已提交
34 35 36
		Name:   cfg.Name,
		isStop: cfg.IsStop,
		isAuth: cfg.IsAuth,
Y
Your Name 已提交
37 38
	}
}
Y
Your Name 已提交
39

Y
Your Name 已提交
40 41 42 43 44
type beforeExecutor struct {
	executorInfo
	plugin goku_plugin.PluginBeforeMatch
}

Y
Your Name 已提交
45 46 47
//Execute execute
func (ex *beforeExecutor) Execute(ctx *common.Context) (isContinue bool, e error) {
	requestID := ctx.RequestId()
Y
Your Name 已提交
48
	ctx.SetPlugin(ex.Name)
Y
Your Name 已提交
49
	log.Debug(requestID, " before plugin :", ex.Name, " start")
Y
Your Name 已提交
50 51
	now := time.Now()
	isContinue, err := ex.plugin.BeforeMatch(ctx)
Y
Your Name 已提交
52 53
	log.Debug(requestID, " before plugin :", ex.Name, " Duration:", time.Since(now))
	log.Debug(requestID, " before plugin :", ex.Name, " end")
Y
Your Name 已提交
54
	if err != nil {
Y
Your Name 已提交
55
		log.Warn(requestID, " before plugin:", ex.Name, " error:", err)
Y
Your Name 已提交
56
	}
Y
Your Name 已提交
57
	return isContinue, err
Y
Your Name 已提交
58 59
}

Y
Your Name 已提交
60 61
//NewBeforeExecutor 创建before阶段执行器
func NewBeforeExecutor(cfg *config.PluginConfig, p goku_plugin.PluginBeforeMatch) *beforeExecutor {
Y
Your Name 已提交
62 63 64 65 66 67
	return &beforeExecutor{
		executorInfo: genExecutor(cfg),
		plugin:       p,
	}

}
Y
Your Name 已提交
68

Y
Your Name 已提交
69 70 71 72 73 74
type accessExecutor struct {
	executorInfo
	plugin goku_plugin.PluginAccess
}

func (ex *accessExecutor) Execute(ctx *common.Context) (isContinue bool, e error) {
Y
Your Name 已提交
75
	requestID := ctx.RequestId()
Y
Your Name 已提交
76 77
	ctx.SetPlugin(ex.Name)

Y
Your Name 已提交
78
	log.Debug(requestID, " access plugin :", ex.Name, " start")
Y
Your Name 已提交
79 80
	now := time.Now()
	isContinue, err := ex.plugin.Access(ctx)
Y
Your Name 已提交
81 82
	log.Debug(requestID, " access plugin :", ex.Name, " Duration:", time.Since(now))
	log.Debug(requestID, " access plugin :", ex.Name, " end")
Y
Your Name 已提交
83
	if err != nil {
Y
Your Name 已提交
84
		log.Warn(requestID, " access plugin:", ex.Name, " error:", err)
Y
Your Name 已提交
85
	}
Y
Your Name 已提交
86
	return isContinue, err
Y
Your Name 已提交
87 88
}

Y
Your Name 已提交
89 90
//NewAccessExecutor 创建access阶段执行器
func NewAccessExecutor(cfg *config.PluginConfig, p goku_plugin.PluginAccess) *accessExecutor {
Y
Your Name 已提交
91 92 93 94 95 96 97 98 99 100 101
	return &accessExecutor{
		executorInfo: genExecutor(cfg),
		plugin:       p,
	}
}

type proxyExecutor struct {
	executorInfo
	plugin goku_plugin.PluginProxy
}

Y
Your Name 已提交
102 103 104
//Execute execute
func (ex *proxyExecutor) Execute(ctx *common.Context) (isContinue bool, e error) {
	requestID := ctx.RequestId()
Y
Your Name 已提交
105 106
	ctx.SetPlugin(ex.Name)

Y
Your Name 已提交
107
	log.Debug(requestID, " proxy plugin :", ex.Name, " start")
Y
Your Name 已提交
108 109
	now := time.Now()
	isContinue, err := ex.plugin.Proxy(ctx)
Y
Your Name 已提交
110 111
	log.Debug(requestID, " proxy plugin :", ex.Name, " Duration:", time.Since(now))
	log.Debug(requestID, " proxy plugin :", ex.Name, " end")
Y
Your Name 已提交
112
	if err != nil {
Y
Your Name 已提交
113
		log.Warn(requestID, " proxy plugin:", ex.Name, " error:", err)
Y
Your Name 已提交
114
	}
Y
Your Name 已提交
115
	return isContinue, err
Y
Your Name 已提交
116 117
}

Y
Your Name 已提交
118 119
//NewProxyExecutor 创建proxy阶段执行器
func NewProxyExecutor(cfg *config.PluginConfig, p goku_plugin.PluginProxy) *proxyExecutor {
Y
Your Name 已提交
120 121 122 123
	return &proxyExecutor{
		executorInfo: genExecutor(cfg),
		plugin:       p,
	}
Y
Your Name 已提交
124
}