plugin.go 3.6 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9
package dao_version_config

import (
	"strconv"

	"github.com/eolinker/goku-api-gateway/config"
)

//GetGlobalPlugin 获取全局插件
Y
Your Name 已提交
10 11
func (d *VersionConfigDao) GetGlobalPlugin() (*config.GatewayPluginConfig, error) {
	db := d.db
Y
Your Name 已提交
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
	sql := "SELECT pluginName,isStop,IFNULL(pluginConfig,''),pluginType FROM goku_plugin"
	rows, err := db.Query(sql)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	pluginConfigs := config.GatewayPluginConfig{
		BeforePlugins: make([]*config.PluginConfig, 0, 20),
		GlobalPlugins: make([]*config.PluginConfig, 0, 20),
	}
	for rows.Next() {
		var pluginName, pluginConfig string
		var isStop bool
		var pluginType int
		err = rows.Scan(&pluginName, &isStop, &pluginConfig, &pluginType)
		if err != nil {
			return nil, err
		}
		if pluginType == 0 {
			pluginConfigs.GlobalPlugins = append(pluginConfigs.GlobalPlugins, &config.PluginConfig{
				Name:   pluginName,
				IsStop: isStop,
				Config: pluginConfig,
			})
		} else {
			pluginConfigs.BeforePlugins = append(pluginConfigs.BeforePlugins, &config.PluginConfig{
				Name:   pluginName,
				IsStop: isStop,
				Config: pluginConfig,
			})
		}
	}
	return &pluginConfigs, nil
}

//GetAPIPlugins 获取接口插件
Y
Your Name 已提交
48 49 50
func (d *VersionConfigDao) GetAPIPlugins() (map[string][]*config.PluginConfig, error) {
	db := d.db
	sql := "SELECT goku_conn_plugin_api.apiID,goku_conn_plugin_api.strategyID,goku_conn_plugin_api.pluginName,goku_conn_plugin_api.pluginConfig,goku_plugin.isStop FROM goku_conn_plugin_api INNER JOIN goku_plugin ON goku_conn_plugin_api.pluginName = goku_plugin.pluginName"
Y
Your Name 已提交
51 52 53 54 55 56 57 58 59
	rows, err := db.Query(sql)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	pluginMaps := make(map[string][]*config.PluginConfig)
	for rows.Next() {
		var apiID int
		var isStop bool
Y
Your Name 已提交
60 61
		var pluginName, pluginConfig, strategyID string
		err = rows.Scan(&apiID, &strategyID, &pluginName, &pluginConfig, &isStop)
Y
Your Name 已提交
62 63 64 65 66 67 68 69
		if err != nil {
			return nil, err
		}
		key := strategyID + ":" + strconv.Itoa(apiID)
		if _, ok := pluginMaps[key]; !ok {
			pluginMaps[key] = make([]*config.PluginConfig, 0, 20)
		}
		pluginMaps[key] = append(pluginMaps[key], &config.PluginConfig{
Y
Your Name 已提交
70 71 72
			Name:   pluginName,
			IsStop: isStop,
			Config: pluginConfig,
Y
Your Name 已提交
73 74 75 76 77 78 79
		})
	}
	return pluginMaps, nil

}

//GetStrategyPlugins 获取策略插件
Y
Your Name 已提交
80 81 82
func (d *VersionConfigDao) GetStrategyPlugins() (map[string][]*config.PluginConfig, map[string]map[string]string, error) {
	db := d.db
	sql := "SELECT goku_conn_plugin_strategy.strategyID,goku_conn_plugin_strategy.pluginName,goku_conn_plugin_strategy.pluginConfig,goku_plugin.isStop FROM goku_conn_plugin_strategy INNER JOIN goku_plugin ON goku_conn_plugin_strategy.pluginName = goku_plugin.pluginName WHERE goku_plugin.pluginStatus = 1 AND goku_conn_plugin_strategy.pluginStatus = 1"
Y
Your Name 已提交
83 84 85 86 87 88 89 90 91
	rows, err := db.Query(sql)
	if err != nil {
		return nil, nil, err
	}
	defer rows.Close()
	pluginMaps := make(map[string][]*config.PluginConfig)
	authMaps := make(map[string]map[string]string)
	for rows.Next() {
		var isStop bool
Y
Your Name 已提交
92 93
		var pluginName, pluginConfig, strategyID string
		err = rows.Scan(&strategyID, &pluginName, &pluginConfig, &isStop)
Y
Your Name 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		if err != nil {
			return nil, nil, err
		}
		key := strategyID
		if _, ok := pluginMaps[key]; !ok {
			pluginMaps[key] = make([]*config.PluginConfig, 0, 20)
		}
		if v, ok := autoAuthNames[pluginName]; ok {
			if _, ok := authMaps[key]; !ok {
				authMaps[key] = make(map[string]string)
			}
			authMaps[key][v] = pluginConfig
		}

		pluginMaps[key] = append(pluginMaps[key], &config.PluginConfig{
Y
Your Name 已提交
109 110 111
			Name:   pluginName,
			IsStop: isStop,
			Config: pluginConfig,
Y
Your Name 已提交
112 113 114 115 116
		})
	}
	return pluginMaps, authMaps, nil

}