executor.go 2.8 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
package plugin_executor

import (
	goku_plugin "github.com/eolinker/goku-plugin"
	"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"
	"time"
)

type Executor interface {
	Execute(ctx *common.Context)(isContinue bool, e error)
	IsStop()bool
}
type executorInfo struct {
	Name string
	isStop bool
}



func (ex*executorInfo) IsStop() bool {
	return  ex.isStop
}

func genExecutor(cfg *config.PluginConfig) executorInfo {
	return executorInfo{
		Name:cfg.Name,
		isStop:cfg.IsStop,
	}
}
type beforeExecutor struct {
	executorInfo
	plugin goku_plugin.PluginBeforeMatch

}

func (ex*beforeExecutor) Execute(ctx *common.Context) (isContinue bool, e error) {
	requestId:=ctx.RequestId()
	ctx.SetPlugin(ex.Name)
	log.Debug(requestId, " before plugin :", ex.Name, " start")
	now := time.Now()
	isContinue, err := ex.plugin.BeforeMatch(ctx)
	log.Debug(requestId, " before plugin :", ex.Name, " Duration:", time.Since(now))
	log.Debug(requestId, " before plugin :", ex.Name, " end")
	if err != nil {
		log.Warn(requestId, " before plugin:", ex.Name, " error:", err)
	}
	return isContinue,err
}

func NewBeforeExecutor(cfg  *config.PluginConfig,p goku_plugin.PluginBeforeMatch ) *beforeExecutor {
	return &beforeExecutor{
		executorInfo: genExecutor(cfg),
		plugin:       p,
	}

}
type accessExecutor struct {
	executorInfo
	plugin goku_plugin.PluginAccess
}

func (ex *accessExecutor) Execute(ctx *common.Context) (isContinue bool, e error) {
	requestId:=ctx.RequestId()
	ctx.SetPlugin(ex.Name)

	log.Debug(requestId, " access plugin :", ex.Name, " start")
	now := time.Now()
	isContinue, err := ex.plugin.Access(ctx)
	log.Debug(requestId, " access plugin :", ex.Name, " Duration:", time.Since(now))
	log.Debug(requestId, " access plugin :", ex.Name, " end")
	if err != nil {
		log.Warn(requestId, " access plugin:", ex.Name, " error:", err)
	}
	return isContinue,err
}

func NewAccessExecutor(cfg  *config.PluginConfig,p goku_plugin.PluginAccess ) *accessExecutor {
	return &accessExecutor{
		executorInfo: genExecutor(cfg),
		plugin:       p,
	}
}

type proxyExecutor struct {
	executorInfo
	plugin goku_plugin.PluginProxy
}

func (ex*proxyExecutor) Execute(ctx *common.Context) (isContinue bool, e error) {
	requestId:=ctx.RequestId()
	ctx.SetPlugin(ex.Name)

	log.Debug(requestId, " proxy plugin :", ex.Name, " start")
	now := time.Now()
	isContinue, err := ex.plugin.Proxy(ctx)
	log.Debug(requestId, " proxy plugin :", ex.Name, " Duration:", time.Since(now))
	log.Debug(requestId, " proxy plugin :", ex.Name, " end")
	if err != nil {
		log.Warn(requestId, " proxy plugin:", ex.Name, " error:", err)
	}
	return isContinue,err
}

func NewProxyExecutor(cfg  *config.PluginConfig,p goku_plugin.PluginProxy ) *proxyExecutor {
	return &proxyExecutor{
		executorInfo: genExecutor(cfg),
		plugin:       p,
	}
}