version.go 3.8 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5
package versionConfig

import (
	"encoding/json"

Y
Your Name 已提交
6 7 8
	"github.com/eolinker/goku-api-gateway/common/pdao"
	"github.com/eolinker/goku-api-gateway/server/dao"
	entity "github.com/eolinker/goku-api-gateway/server/entity/console-entity"
Y
Your Name 已提交
9

Y
Your Name 已提交
10
	"github.com/eolinker/goku-api-gateway/ksitigarbha"
Y
Your Name 已提交
11 12 13 14

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

Y
Your Name 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
var (
	versionDao       dao.VersionDao
	versionConfigDao dao.VersionConfigDao
	clusterDao       dao.ClusterDao
	authNames        = map[string]string{
		"Oauth2": "goku-oauth2_auth",
		"Apikey": "goku-apikey_auth",
		"Basic":  "goku-basic_auth",
		"Jwt":    "goku-jwt_auth",
	}
)

func init() {
	pdao.Need(&versionConfigDao, &versionDao, &clusterDao)
Y
Your Name 已提交
29 30 31 32
}

//GetVersionList 获取版本列表
func GetVersionList(keyword string) ([]config.VersionConfig, error) {
Y
Your Name 已提交
33
	return versionDao.GetVersionList(keyword)
Y
Your Name 已提交
34 35 36
}

//AddVersionConfig 新增版本配置
Y
Your Name 已提交
37
func AddVersionConfig(name, version, remark, now string, userID int) (int, error) {
Y
Your Name 已提交
38
	config, balanceConfig, discoverConfig := buildVersionConfig(version)
Y
Your Name 已提交
39 40 41 42
	return versionDao.AddVersionConfig(name, version, remark, config, balanceConfig, discoverConfig, now, userID)
}
func EditVersionBasicConfig(name, version, remark string, userID, versionID int) error {
	return versionDao.EditVersionBasicConfig(name, version, remark, userID, versionID)
Y
Your Name 已提交
43 44 45 46
}

//BatchDeleteVersionConfig 批量删除版本配置
func BatchDeleteVersionConfig(ids []int) error {
Y
Your Name 已提交
47 48
	publishID := versionDao.GetPublishVersionID()
	return versionDao.BatchDeleteVersionConfig(ids, publishID)
Y
Your Name 已提交
49 50 51
}

//PublishVersion 发布版本
Y
Your Name 已提交
52 53
func PublishVersion(id, userID int, now string) error {
	err := versionDao.PublishVersion(id, userID, now)
Y
Your Name 已提交
54 55 56 57 58 59 60 61
	if err == nil {
		load()
	}
	return err
}

//GetVersionConfigCount 获取版本配置数量
func GetVersionConfigCount() int {
Y
Your Name 已提交
62 63 64 65 66 67 68 69 70
	return versionDao.GetVersionConfigCount()
}

func getRedisConfig(clusters []*entity.Cluster) map[string]interface{} {
	redisConfig := map[string]interface{}{}
	for _, c := range clusters {
		redisConfig[c.Name] = c.Redis
	}
	return redisConfig
Y
Your Name 已提交
71 72 73
}

func buildVersionConfig(v string) (string, string, string) {
Y
Your Name 已提交
74
	clusters, err := clusterDao.GetClusters()
Y
Your Name 已提交
75 76 77
	if err != nil {
		return "", "", ""
	}
Y
Your Name 已提交
78
	discoverMap, err := versionConfigDao.GetDiscoverConfig(clusters)
Y
Your Name 已提交
79 80 81
	if err != nil {
		return "", "", ""
	}
Y
Your Name 已提交
82
	balanceMap, err := versionConfigDao.GetBalances(clusters)
Y
Your Name 已提交
83 84 85
	if err != nil {
		return "", "", ""
	}
Y
Your Name 已提交
86
	openStrategy, strategyConfigs, err := versionConfigDao.GetStrategyConfig()
Y
Your Name 已提交
87 88 89
	if err != nil {
		return "", "", ""
	}
Y
Your Name 已提交
90
	apiContents, err := versionConfigDao.GetAPIContent()
Y
Your Name 已提交
91 92 93
	if err != nil {
		return "", "", ""
	}
Y
Your Name 已提交
94
	plugins, err := versionConfigDao.GetGlobalPlugin()
Y
Your Name 已提交
95 96 97
	if err != nil {
		return "", "", ""
	}
Y
Your Name 已提交
98
	logCf, accessCf, err := versionConfigDao.GetLogInfo()
Y
Your Name 已提交
99 100 101
	if err != nil {
		return "", "", ""
	}
Y
Your Name 已提交
102 103 104

	g, _ := versionConfigDao.GetGatewayBasicConfig()
	routers, _ := versionConfigDao.GetRouterRules(1)
Y
Your Name 已提交
105
	ms := make(map[string]string)
Y
Your Name 已提交
106
	modules, _ := versionConfigDao.GetMonitorModules(1, false)
Y
Your Name 已提交
107 108
	if modules != nil {
		for key, config := range modules {
Y
Your Name 已提交
109 110
			module, has := ksitigarbha.GetMonitorModuleModel(key)
			if has {
Y
Your Name 已提交
111 112 113 114
				ms[module.GetName()] = config
			}
		}
	}
Y
Your Name 已提交
115 116 117

	c := config.GokuConfig{
		Version:             v,
Y
Your Name 已提交
118
		Plugins:             *plugins,
Y
Your Name 已提交
119 120 121 122 123 124
		APIS:                apiContents,
		Strategy:            strategyConfigs,
		AnonymousStrategyID: openStrategy,
		AuthPlugin:          authNames,
		Log:                 logCf,
		AccessLog:           accessCf,
Y
Your Name 已提交
125
		MonitorModules:      ms,
Y
Your Name 已提交
126 127 128
		Routers:             routers,
		GatewayBasicInfo:    g,
		RedisConfig:         getRedisConfig(clusters),
Y
Your Name 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	}

	cByte, err := json.Marshal(c)
	if err != nil {
		return "", "", ""
	}
	bByte, err := json.Marshal(balanceMap)
	if err != nil {
		return "", "", ""
	}
	dByte, err := json.Marshal(discoverMap)
	if err != nil {
		return "", "", ""
	}

	return string(cByte), string(bByte), string(dByte)
}