strategy-api.go 1.3 KB
Newer Older
Y
Your Name 已提交
1
package strategyapimanager
E
eoLinker API Management 已提交
2 3 4 5

import (
	"sync"

黄孟柱 已提交
6 7 8
	"github.com/eolinker/goku-api-gateway/goku-node/manager/updater"
	dao_strategy "github.com/eolinker/goku-api-gateway/server/dao/node-mysql/dao-strategy"
	entity "github.com/eolinker/goku-api-gateway/server/entity/node-entity"
E
eoLinker API Management 已提交
9 10 11
)

func init() {
Y
Your Name 已提交
12
	updater.Add(loadStategyAPI, 6, "goku_conn_strategy_api", "goku_gateway_strategy", "goku_gateway_api")
E
eoLinker API Management 已提交
13 14 15
}

var (
Y
Your Name 已提交
16
	apiOfStrategy = make(map[string]*_APIMap)
E
eoLinker API Management 已提交
17 18 19 20
	apiLocker     sync.RWMutex
)

//GetAPIForStategy 获取指定策略、API 对应的 配置
Y
Your Name 已提交
21 22
func GetAPIForStategy(stategyID string, apiID int) (*entity.StrategyAPI, bool) {
	apis, has := getAPis(stategyID)
E
eoLinker API Management 已提交
23 24 25
	if !has {
		return nil, false
	}
Y
Your Name 已提交
26
	return apis.Get(apiID)
E
eoLinker API Management 已提交
27 28
}

Y
Your Name 已提交
29 30
func getAPis(strategyID string) (*_APIMap, bool) {
	apis, has := apiOfStrategy[strategyID]
E
eoLinker API Management 已提交
31 32
	return apis, has
}
Y
Your Name 已提交
33
func resetAPIs(apis map[string]*_APIMap) {
E
eoLinker API Management 已提交
34 35 36 37 38
	apiLocker.Lock()
	defer apiLocker.Unlock()

	apiOfStrategy = apis
}
Y
Your Name 已提交
39 40
func loadStategyAPI() {
	apis, e := dao_strategy.GetAllStrategyAPI()
E
eoLinker API Management 已提交
41 42 43 44
	if e != nil {
		return
	}

Y
Your Name 已提交
45
	tem := make(map[string]*_APIMap)
E
eoLinker API Management 已提交
46 47 48 49
	for _, api := range apis {

		as, has := tem[api.StrategyID]
		if !has {
Y
Your Name 已提交
50 51
			as = &_APIMap{
				apis: make(map[int]*entity.StrategyAPI),
E
eoLinker API Management 已提交
52 53
			}
		}
Y
Your Name 已提交
54
		as.apis[api.APIID] = api
E
eoLinker API Management 已提交
55 56
		tem[api.StrategyID] = as
	}
Y
Your Name 已提交
57
	resetAPIs(tem)
E
eoLinker API Management 已提交
58
}