plugin.go 1.9 KB
Newer Older
Y
Your Name 已提交
1 2 3 4
package gateway

import (
	"reflect"
Y
Your Name 已提交
5
	"strings"
Y
Your Name 已提交
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

	"github.com/eolinker/goku-api-gateway/config"
	plugin_executor "github.com/eolinker/goku-api-gateway/node/gateway/plugin-executor"
	plugin "github.com/eolinker/goku-api-gateway/node/plugin-loader"
)

func genBeforPlugin(cfgs []*config.PluginConfig, cluster string) []plugin_executor.Executor {
	ps := make([]plugin_executor.Executor, 0, len(cfgs))
	for _, cfg := range cfgs {

		factory, e := plugin.LoadPlugin(cfg.Name)
		if e != nil {
			continue
		}
		obj, err := factory.Create(cfg.Config, cluster, cfg.UpdateTag, "", 0)
		if err != nil {
			continue
		}

		if obj.BeforeMatch != nil && !reflect.ValueOf(obj.BeforeMatch).IsNil() {
			ps = append(ps, plugin_executor.NewBeforeExecutor(cfg, obj.BeforeMatch))
		}
	}
	return ps
}

func genPlugins(cfgs []*config.PluginConfig, cluster string, strategyID string, apiID int) ([]plugin_executor.Executor, []plugin_executor.Executor, []plugin_executor.Executor) {
	psBefor := make([]plugin_executor.Executor, 0, len(cfgs))
	psAccess := make([]plugin_executor.Executor, 0, len(cfgs))
	psProxy := make([]plugin_executor.Executor, 0, len(cfgs))
	for _, cfg := range cfgs {

		factory, e := plugin.LoadPlugin(cfg.Name)
		if e != nil {
			continue
		}
		obj, err := factory.Create(cfg.Config, cluster, cfg.UpdateTag, strategyID, apiID)
		if err != nil {
			continue
		}

		if obj.BeforeMatch != nil && !reflect.ValueOf(obj.BeforeMatch).IsNil() {
			psBefor = append(psBefor, plugin_executor.NewBeforeExecutor(cfg, obj.BeforeMatch))
		}
		if obj.Access != nil && !reflect.ValueOf(obj.Access).IsNil() {
Y
Your Name 已提交
51 52 53
			if strings.Contains(cfg.Name, "_auth") {
				cfg.IsAuth = true
			}
Y
Your Name 已提交
54 55 56 57 58 59 60 61 62
			psAccess = append(psAccess, plugin_executor.NewAccessExecutor(cfg, obj.Access))
		}
		if obj.Proxy != nil && !reflect.ValueOf(obj.Proxy).IsNil() {
			psProxy = append(psProxy, plugin_executor.NewProxyExecutor(cfg, obj.Proxy))
		}
	}
	return psBefor, psAccess, psProxy

}