apiPlugin.go 10.3 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package api

import (
	"encoding/json"
	"net/http"
	"strconv"

黄孟柱 已提交
8 9 10 11 12
	"github.com/eolinker/goku-api-gateway/console/controller"
	"github.com/eolinker/goku-api-gateway/console/module/api"
	"github.com/eolinker/goku-api-gateway/console/module/plugin"
	plugin_config "github.com/eolinker/goku-api-gateway/console/module/plugin/plugin-config"
	"github.com/eolinker/goku-api-gateway/console/module/strategy"
Y
Your Name 已提交
13
	goku_handler "github.com/eolinker/goku-api-gateway/goku-handler"
E
eoLinker API Management 已提交
14 15
)

Y
Your Name 已提交
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
const operationAPIPlugin = "strategyManagement"

//PluginHandlers 插件处理器
type PluginHandlers struct {
}

//Handlers handlers
func (p *PluginHandlers) Handlers(factory *goku_handler.AccountHandlerFactory) map[string]http.Handler {
	return map[string]http.Handler{
		"/addPluginToApi":    factory.NewAccountHandleFunction(operationAPIPlugin, true, AddPluginToAPI),
		"/edit":              factory.NewAccountHandleFunction(operationAPIPlugin, true, EditAPIPluginConfig),
		"/getInfo":           factory.NewAccountHandleFunction(operationAPIPlugin, false, GetAPIPluginConfig),
		"/getList":           factory.NewAccountHandleFunction(operationAPIPlugin, false, GetAPIPluginList),
		"/getListByStrategy": factory.NewAccountHandleFunction(operationAPIPlugin, false, GetAllAPIPluginInStrategy),
		"/batchStart":        factory.NewAccountHandleFunction(operationAPIPlugin, true, BatchStartAPIPlugin),
		"/batchStop":         factory.NewAccountHandleFunction(operationAPIPlugin, true, BatchStopAPIPlugin),
		"/batchDelete":       factory.NewAccountHandleFunction(operationAPIPlugin, true, BatchDeleteAPIPlugin),
		"/notAssign/getList": factory.NewAccountHandleFunction(operationAPIPlugin, false, GetAPIPluginListWithNotAssignAPIList),
	}
}

//NewPluginHandlers new插件处理器
func NewPluginHandlers() *PluginHandlers {
	return &PluginHandlers{}

}

Y
Your Name 已提交
43 44
//AddPluginToAPI 新增插件到接口
func AddPluginToAPI(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
45 46 47 48 49

	pluginName := httpRequest.PostFormValue("pluginName")
	pluginConfig := httpRequest.PostFormValue("pluginConfig")
	strategyID := httpRequest.PostFormValue("strategyID")
	apiID := httpRequest.PostFormValue("apiID")
Y
Your Name 已提交
50
	userID := goku_handler.UserIDFromRequest(httpRequest)
E
eoLinker API Management 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	aID, err := strconv.Atoi(apiID)
	if err != nil {
		controller.WriteError(httpResponse, "240002", "apiPlugin", "[ERROR]Illegal apiID!", err)
		return
	}
	flag, err := plugin_config.CheckConfig(pluginName, []byte(pluginConfig))
	if !flag {
		controller.WriteError(httpResponse, "500000", "apiPlugin", "[ERROR]插件配置无效:"+err.Error(), err)
		return
	}

	// 查询该插件是否存在
	flag, err = plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse, "240005", "apiPlugin", "[ERROR]The plugin does not exist!", err)
		return
	}
Y
Your Name 已提交
68
	flag, err = api.CheckAPIIsExist(aID)
E
eoLinker API Management 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82
	if !flag {

		controller.WriteError(httpResponse, "240012", "apiPlugin", "[ERROR]The api does not exist!", err)
		return
	}

	flag, err = strategy.CheckStrategyIsExist(strategyID)
	if !flag {

		controller.WriteError(httpResponse, "240013", "apiPlugin", "[ERROR]The strategy does not exist!", err)
		return
	}

	id := 0
Y
Your Name 已提交
83
	exist, err := api.CheckPluginIsExistInAPI(strategyID, pluginName, aID)
E
eoLinker API Management 已提交
84 85

	if exist {
Y
Your Name 已提交
86
		flag, resultDesc, err := api.EditAPIPluginConfig(pluginName, pluginConfig, strategyID, aID, userID)
E
eoLinker API Management 已提交
87 88 89 90 91 92
		if !flag {
			controller.WriteError(httpResponse, "240000", "apiPlugin", resultDesc.(string), err)
			return
		}
		id = resultDesc.(int)
	} else {
Y
Your Name 已提交
93
		flag, resultDesc, err := api.AddPluginToAPI(pluginName, pluginConfig, strategyID, aID, userID)
E
eoLinker API Management 已提交
94 95 96 97 98 99 100 101 102 103 104 105
		if !flag {
			controller.WriteError(httpResponse, "240000", "apiPlugin", resultDesc.(string), err)
			return
		}
		id = resultDesc.(int)
	}

	controller.WriteResultInfo(httpResponse, "apiPlugin", "connId", id)

	return
}

Y
Your Name 已提交
106 107
//EditAPIPluginConfig 修改接口插件
func EditAPIPluginConfig(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
108 109 110 111 112

	pluginName := httpRequest.PostFormValue("pluginName")
	pluginConfig := httpRequest.PostFormValue("pluginConfig")
	strategyID := httpRequest.PostFormValue("strategyID")
	apiID := httpRequest.PostFormValue("apiID")
Y
Your Name 已提交
113 114
	userID := goku_handler.UserIDFromRequest(httpRequest)

E
eoLinker API Management 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	flag, err := plugin_config.CheckConfig(pluginName, []byte(pluginConfig))
	if !flag {
		controller.WriteError(httpResponse, "500000", "apiPlugin", "[ERROR]插件配置无效:"+err.Error(), err)
		return
	}
	aID, err := strconv.Atoi(apiID)
	if err != nil {
		controller.WriteError(httpResponse,
			"240002",
			"apiPlugin",
			"[ERROR]Illegal apiID!",
			err)
		return

	}

	// 查询该插件是否存在
	flag, err = plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse,
			"200005",
			"apiPlugin",
			"[ERROR]The plugin does not exist!",
			err)
		return

	}
Y
Your Name 已提交
142
	flag, err = api.CheckAPIIsExist(aID)
E
eoLinker API Management 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	if !flag {
		controller.WriteError(httpResponse,
			"240012",
			"apiPlugin",
			"[ERROR]The api does not exist!",
			err)
		return

	}
	flag, err = strategy.CheckStrategyIsExist(strategyID)
	if !flag {
		controller.WriteError(httpResponse,
			"240013",
			"apiPlugin",
			"[ERROR]The strategy does not exist!",
			err)
		return

	}
Y
Your Name 已提交
162
	flag, resultDesc, err := api.EditAPIPluginConfig(pluginName, pluginConfig, strategyID, aID, userID)
E
eoLinker API Management 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175
	if !flag {

		controller.WriteError(httpResponse,
			"240000",
			"apiPlugin",
			resultDesc.(string),
			err)
	}

	controller.WriteResultInfo(httpResponse, "apiPlugin", "", nil)
	return
}

Y
Your Name 已提交
176 177
//GetAPIPluginConfig 获取接口插件配置
func GetAPIPluginConfig(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

	pluginName := httpRequest.PostFormValue("pluginName")
	strategyID := httpRequest.PostFormValue("strategyID")
	apiID := httpRequest.PostFormValue("apiID")

	aID, err := strconv.Atoi(apiID)
	if err != nil {
		controller.WriteError(httpResponse,
			"240002",
			"apiPlugin",
			"[ERROR]Illegal apiID!",
			err)
		return

	}
Y
Your Name 已提交
193
	flag, result, err := api.GetAPIPluginConfig(aID, strategyID, pluginName)
E
eoLinker API Management 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	if !flag {
		resultByte, _ := json.Marshal(map[string]interface{}{
			"type":            "apiPlugin",
			"statusCode":      "000000",
			"apiPluginConfig": "",
			// "apiName":         result["apiName"],
			// "requestURL":      result["requestURL"],
		})
		httpResponse.Write(resultByte)
		return
	}
	resultByte, _ := json.Marshal(map[string]interface{}{
		"type":            "apiPlugin",
		"statusCode":      "000000",
		"apiPluginConfig": result["pluginConfig"],
		"apiName":         result["apiName"],
		"requestURL":      result["requestURL"],
	})
	httpResponse.Write(resultByte)
	return

}

Y
Your Name 已提交
217 218
//GetAPIPluginList 获取接口插件配置
func GetAPIPluginList(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231

	strategyID := httpRequest.PostFormValue("strategyID")
	apiID := httpRequest.PostFormValue("apiID")
	aID, err := strconv.Atoi(apiID)
	if err != nil {
		controller.WriteError(httpResponse,
			"240002",
			"apiPlugin",
			"[ERROR]Illegal apiID!",
			err)
		return

	}
Y
Your Name 已提交
232
	flag, result, err := api.GetAPIPluginList(aID, strategyID)
E
eoLinker API Management 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246
	if !flag {
		controller.WriteError(httpResponse,
			"240000",
			"apiPlugin",
			"[ERROR]Empty plugin list!",
			err)
		return

	}

	controller.WriteResultInfo(httpResponse, "apiPlugin", "apiPluginList", result)
	return
}

Y
Your Name 已提交
247 248
//GetAllAPIPluginInStrategy 获取策略组中所有接口插件列表
func GetAllAPIPluginInStrategy(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
249 250 251

	strategyID := httpRequest.PostFormValue("strategyID")

Y
Your Name 已提交
252
	flag, result, err := api.GetAllAPIPluginInStrategy(strategyID)
E
eoLinker API Management 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	if !flag {

		controller.WriteError(httpResponse,
			"240000",
			"apiPlugin",
			"[ERROR]Empty api plugin list!",
			err)
		return

	}

	controller.WriteResultInfo(httpResponse, "apiPlugin", "apiPluginList", result)
	return
}

Y
Your Name 已提交
268 269
//BatchStartAPIPlugin 批量修改策略组插件状态
func BatchStartAPIPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
270 271 272

	strategyID := httpRequest.PostFormValue("strategyID")
	connIDList := httpRequest.PostFormValue("connIDList")
Y
Your Name 已提交
273 274
	userID := goku_handler.UserIDFromRequest(httpRequest)

E
eoLinker API Management 已提交
275 276 277 278 279 280 281 282 283
	if connIDList == "" {
		controller.WriteError(httpResponse,
			"240001",
			"apiPlugin",
			"[ERROR]Illegal connIDList",
			nil)
		return

	}
Y
Your Name 已提交
284
	flag, result, err := api.BatchEditAPIPluginStatus(connIDList, strategyID, 1, userID)
E
eoLinker API Management 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if !flag {

		controller.WriteError(httpResponse,
			"240000",
			"apiPlugin",
			result,
			err)
		return
	}

	controller.WriteResultInfo(httpResponse, "apiPlugin", "", nil)
	return
}

Y
Your Name 已提交
299 300
//BatchStopAPIPlugin 批量修改策略组插件状态
func BatchStopAPIPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
301 302 303

	strategyID := httpRequest.PostFormValue("strategyID")
	connIDList := httpRequest.PostFormValue("connIDList")
Y
Your Name 已提交
304 305
	userID := goku_handler.UserIDFromRequest(httpRequest)

E
eoLinker API Management 已提交
306 307 308 309 310 311 312 313 314
	if connIDList == "" {
		controller.WriteError(httpResponse,
			"240001",
			"apiPlugin",
			"[ERROR]Illegal connIDList",
			nil)
		return

	}
Y
Your Name 已提交
315
	flag, _, err := api.BatchEditAPIPluginStatus(connIDList, strategyID, 0, userID)
E
eoLinker API Management 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329
	if !flag {

		controller.WriteError(httpResponse,
			"240000",
			"apiPlugin",
			"[ERROR]The api plugin is stoped!",
			err)
		return

	}
	controller.WriteResultInfo(httpResponse, "apiPlugin", "", nil)
	return
}

Y
Your Name 已提交
330 331
//BatchDeleteAPIPlugin 批量删除策略组插件
func BatchDeleteAPIPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
332 333 334

	strategyID := httpRequest.PostFormValue("strategyID")
	connIDList := httpRequest.PostFormValue("connIDList")
Y
Your Name 已提交
335

E
eoLinker API Management 已提交
336 337 338 339 340 341 342 343 344
	if connIDList == "" {
		controller.WriteError(httpResponse,
			"240001",
			"apiPlugin",
			"[ERROR]Illegal connIDList",
			nil)
		return

	}
Y
Your Name 已提交
345
	flag, result, err := api.BatchDeleteAPIPlugin(connIDList, strategyID)
E
eoLinker API Management 已提交
346 347 348 349 350 351 352 353 354 355 356
	if !flag {

		controller.WriteError(httpResponse,
			"240000",
			"apiPlugin",
			result,
			err)
	}
	controller.WriteResultInfo(httpResponse, "apiPlugin", "", nil)
}

Y
Your Name 已提交
357
//GetAPIPluginListWithNotAssignAPIList 获取没有分配接口插件的接口列表
Y
Your Name 已提交
358
func GetAPIPluginListWithNotAssignAPIList(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
359 360 361 362 363 364 365 366 367 368

	strategyID := httpRequest.PostFormValue("strategyID")
	if strategyID == "" {
		controller.WriteError(httpResponse,
			"240003",
			"apiPlugin",
			"[ERROR]Illegal strategyID",
			nil)
		return
	}
Y
Your Name 已提交
369
	flag, result, err := api.GetAPIPluginListWithNotAssignAPIList(strategyID)
E
eoLinker API Management 已提交
370 371 372 373 374 375 376 377 378 379 380 381
	if !flag {

		controller.WriteError(httpResponse,
			"240000",
			"apiPlugin",
			err.Error(),
			err)

	}
	controller.WriteResultInfo(httpResponse, "apiPlugin", "pluginList", result)

}