api.go 1.0 KB
Newer Older
Y
Your Name 已提交
1
package apimanager
E
eoLinker API Management 已提交
2 3 4 5

import (
	"sync"

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

func init() {
	updater.Add(load, 5, "goku_gateway_api")
}

// apiList: api列表全局变量
var (
Y
Your Name 已提交
17
	apiList map[int]*entity.API
E
eoLinker API Management 已提交
18 19 20
	locker  sync.RWMutex
)

Y
Your Name 已提交
21 22
//GetAPI 通过id获取API信息
func GetAPI(id int) (*entity.API, bool) {
E
eoLinker API Management 已提交
23 24 25 26 27 28
	locker.RLock()
	defer locker.RUnlock()
	a, has := apiList[id]
	return a, has
}

Y
Your Name 已提交
29 30
//GetAPIs 获取接口信息列表
func GetAPIs(ids []int) []*entity.API {
E
eoLinker API Management 已提交
31

Y
Your Name 已提交
32
	apis := make([]*entity.API, 0, len(ids))
E
eoLinker API Management 已提交
33 34 35 36 37 38 39 40 41 42 43 44

	locker.RLock()
	defer locker.RUnlock()
	for _, id := range ids {
		if api, has := apiList[id]; has {
			apis = append(apis, api)
		}
	}

	return apis
}

Y
Your Name 已提交
45
func reset(list map[int]*entity.API) {
E
eoLinker API Management 已提交
46 47 48 49 50 51
	locker.Lock()
	defer locker.Unlock()
	apiList = list
}

func load() {
Y
Your Name 已提交
52
	apis, e := dao_api.GetAllAPI()
E
eoLinker API Management 已提交
53 54 55 56 57
	if e != nil {
		return
	}
	reset(apis)
}