strategy.go 2.6 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package dao_version_config

import (
	"strconv"

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

var autoAuthNames = map[string]string{
	"goku-oauth2_auth": "Oauth2",
	"goku-apikey_auth": "Apikey",
	"goku-basic_auth":  "Basic",
	"goku-jwt_auth":    "Jwt",
}

//GetAPIsOfStrategy 获取策略内接口数据
Y
Your Name 已提交
17 18
func (d *VersionConfigDao)GetAPIsOfStrategy() (map[string][]*config.APIOfStrategy, error) {
	db := d.db
Y
Your Name 已提交
19 20 21 22 23 24
	sql := "SELECT goku_conn_strategy_api.apiID,IFNULL(goku_conn_strategy_api.target,''),goku_conn_strategy_api.strategyID FROM goku_conn_strategy_api;"
	rows, err := db.Query(sql)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
Y
Your Name 已提交
25
	apiPlugins, err := d.GetAPIPlugins()
Y
Your Name 已提交
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
	if err != nil {
		return nil, err
	}
	apiMaps := make(map[string][]*config.APIOfStrategy)
	for rows.Next() {
		var apiID int
		var balanceName, strategyID string
		err = rows.Scan(&apiID, &balanceName, &strategyID)
		if err != nil {
			return nil, err
		}
		if _, ok := apiMaps[strategyID]; !ok {
			apiMaps[strategyID] = make([]*config.APIOfStrategy, 0, 20)
		}
		ap := make([]*config.PluginConfig, 0)
		key := strategyID + ":" + strconv.Itoa(apiID)
		if v, ok := apiPlugins[key]; ok {
			ap = v
		}
		apiMaps[strategyID] = append(apiMaps[strategyID], &config.APIOfStrategy{
			ID:      apiID,
			Balance: balanceName,
			Plugins: ap,
		})
	}
	return apiMaps, nil
}

//GetStrategyConfig 获取策略配置
Y
Your Name 已提交
55 56
func (d *VersionConfigDao)GetStrategyConfig() (string, []*config.StrategyConfig, error) {
	db := d.db
Y
Your Name 已提交
57 58 59 60 61 62 63 64
	sql := "SELECT strategyID,strategyName,enableStatus,strategyType FROM goku_gateway_strategy"

	rows, err := db.Query(sql)
	if err != nil {
		return "", nil, err
	}
	defer rows.Close()
	strategyConfigs := make([]*config.StrategyConfig, 0, 20)
Y
Your Name 已提交
65
	strategyPlugins, authMaps, err := d.GetStrategyPlugins()
Y
Your Name 已提交
66 67 68
	if err != nil {
		return "", nil, err
	}
Y
Your Name 已提交
69
	apiOfStrategy, err := d.GetAPIsOfStrategy()
Y
Your Name 已提交
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
	if err != nil {
		return "", nil, err
	}
	openStrategy := ""
	for rows.Next() {
		var strategyConfig config.StrategyConfig
		var strategyType int
		err = rows.Scan(&strategyConfig.ID, &strategyConfig.Name, &strategyConfig.Enable, &strategyType)
		if err != nil {
			return "", nil, err
		}
		if _, ok := strategyPlugins[strategyConfig.ID]; ok {
			strategyConfig.Plugins = strategyPlugins[strategyConfig.ID]
		}
		if _, ok := authMaps[strategyConfig.ID]; ok {
			strategyConfig.AUTH = authMaps[strategyConfig.ID]
		}
		if _, ok := apiOfStrategy[strategyConfig.ID]; ok {
			strategyConfig.APIS = apiOfStrategy[strategyConfig.ID]
		}
		if strategyType == 1 {
			openStrategy = strategyConfig.ID
		}
		strategyConfigs = append(strategyConfigs, &strategyConfig)
	}
	return openStrategy, strategyConfigs, nil

}