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

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

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

	"github.com/pkg/errors"

	goku_handler "github.com/eolinker/goku-api-gateway/goku-handler"

黄孟柱 已提交
15 16
	"github.com/eolinker/goku-api-gateway/console/controller"
	"github.com/eolinker/goku-api-gateway/console/module/api"
E
eoLinker API Management 已提交
17 18
)

Y
Your Name 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
const operationAPI = "apiManagement"

//Handlers handlers
type Handlers struct {
}

//Handlers handlers
func (h *Handlers) Handlers(factory *goku_handler.AccountHandlerFactory) map[string]http.Handler {
	return map[string]http.Handler{
		"/add":              factory.NewAccountHandleFunction(operationAPI, true, AddAPI),
		"/edit":             factory.NewAccountHandleFunction(operationAPI, true, EditAPI),
		"/copy":             factory.NewAccountHandleFunction(operationAPI, true, CopyAPI),
		"/getInfo":          factory.NewAccountHandleFunction(operationAPI, false, GetAPIInfo),
		"/getList":          factory.NewAccountHandleFunction(operationAPI, false, GetAPIList),
		"/id/getList":       factory.NewAccountHandleFunction(operationAPI, false, GetAPIIDList),
		"/batchEditGroup":   factory.NewAccountHandleFunction(operationAPI, true, BatchEditAPIGroup),
		"/batchDelete":      factory.NewAccountHandleFunction(operationAPI, true, BatchDeleteAPI),
		"/batchEditBalance": factory.NewAccountHandleFunction(operationAPI, true, BatchSetBalanceAPI),
	}
}

//NewAPIHandlers API处理器
func NewAPIHandlers() *Handlers {
	return &Handlers{}
}

Y
Your Name 已提交
45 46
//AddAPI 新增接口
func AddAPI(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
47 48

	apiName := httpRequest.PostFormValue("apiName")
Y
Your Name 已提交
49
	alias := httpRequest.PostFormValue("alias")
E
eoLinker API Management 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
	requestURL := httpRequest.PostFormValue("requestURL")
	requestMethod := httpRequest.PostFormValue("requestMethod")
	protocol := httpRequest.PostFormValue("protocol")
	balanceName := httpRequest.PostFormValue("balanceName")
	targetURL := httpRequest.PostFormValue("targetURL")
	targetMethod := httpRequest.PostFormValue("targetMethod")
	isFollow := httpRequest.PostFormValue("isFollow")
	timeout := httpRequest.PostFormValue("timeout")
	retryCount := httpRequest.PostFormValue("retryCount")
	groupID := httpRequest.PostFormValue("groupID")
	projectID := httpRequest.PostFormValue("projectID")
	alertValve := httpRequest.PostFormValue("alertValve")
	managerID := httpRequest.PostFormValue("managerID")
Y
Your Name 已提交
63 64 65 66
	apiType := httpRequest.PostFormValue("apiType")
	linkApis := httpRequest.PostFormValue("linkApis")
	staticResponse := httpRequest.PostFormValue("staticResponse")
	responseDataType := httpRequest.PostFormValue("responseDataType")
Y
Your Name 已提交
67
	userID := goku_handler.UserIDFromRequest(httpRequest)
E
eoLinker API Management 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
	if apiName == "" {
		controller.WriteError(httpResponse, "190002", "api", "[ERROR]Illegal apiName!", nil)
		return
	}
	if isFollow != "true" && isFollow != "false" && isFollow != "" {
		controller.WriteError(httpResponse, "190008", "api", "[ERROR]Illegal isFollow!", nil)
		return

	}
	if isFollow == "" {
		isFollow = "false"
	}

Y
Your Name 已提交
81 82 83 84
	aType, err := strconv.Atoi(apiType)
	if err != nil && apiType == "" {
		controller.WriteError(httpResponse, "190012", "api", "[ERROR]Illegal apiType!", err)
		return
E
eoLinker API Management 已提交
85
	}
Y
Your Name 已提交
86 87 88 89 90 91 92 93 94

	if !utils.ValidateURL(requestURL) {
		controller.WriteError(httpResponse, "190021", "api", "[ERROR]Illegal requestURL!", nil)
		return
	}
	if aType == 1 && !utils.ValidateURL(targetURL) {
		controller.WriteError(httpResponse, "190022", "api", "[ERROR]Illegal requestURL!", nil)
		return
	}
Y
Your Name 已提交
95 96
	if responseDataType != "origin" && responseDataType != "json" && responseDataType != "xml" {
		controller.WriteError(httpResponse, "190013", "api", "[ERROR]Illegal responseDataType!", err)
E
eoLinker API Management 已提交
97 98 99 100 101 102 103
		return
	}
	t, err := strconv.Atoi(timeout)
	if err != nil && timeout != "" {
		controller.WriteError(httpResponse, "190010", "api", "[ERROR]Illegal timeout!", err)
		return
	}
Y
Your Name 已提交
104

E
eoLinker API Management 已提交
105 106 107 108 109 110 111 112 113 114 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
	count, err := strconv.Atoi(retryCount)
	if err != nil && retryCount != "" {
		controller.WriteError(httpResponse, "190011", "api", "[ERROR]Illegal retryCount!", err)
		return

	}
	if t < 1 {
		controller.WriteError(httpResponse, "190010", "api", "[ERROR]Illegal timeout!", nil)
		return
	}
	gID, err := strconv.Atoi(groupID)
	if err != nil {
		controller.WriteError(httpResponse, "190015", "api", "[ERROR]Illegal groupID!", err)
		return

	}
	pjID, err := strconv.Atoi(projectID)
	if err != nil {
		controller.WriteError(httpResponse, "190016", "api", "[ERROR]Illegal projectID!", err)
		return

	}
	apiValve, err := strconv.Atoi(alertValve)
	if err != nil && alertValve != "" {
		controller.WriteError(httpResponse, "190017", "api", "[ERROR]Illegal alertValve!", err)
		return

	}
	mgID, err := strconv.Atoi(managerID)
	if (err != nil && managerID != "") || mgID < -1 {
		controller.WriteError(httpResponse, "190018", "api", "[ERROR]Illegal managerID!", err)
		return

	}
	if managerID == "" {
		mgID = userID
	}
Y
Your Name 已提交
142 143 144 145 146
	if api.CheckAliasIsExist(0, alias) {
		errInfo := "[ERROR]duplicate alias!"
		controller.WriteError(httpResponse, "190020", "api", errInfo, errors.New(errInfo))
		return
	}
E
eoLinker API Management 已提交
147

Y
Your Name 已提交
148
	flag, id, err := api.AddAPI(apiName, alias, requestURL, targetURL, requestMethod, targetMethod, isFollow, linkApis, staticResponse, responseDataType, balanceName, protocol, pjID, gID, t, count, apiValve, mgID, userID, aType)
E
eoLinker API Management 已提交
149 150 151 152 153 154 155 156 157 158 159
	if !flag {

		controller.WriteError(httpResponse,
			"190000", "api", "[ERROR]URL Repeat!", err)
		return
	}

	controller.WriteResultInfo(httpResponse, "api", "apiID", id)
	return
}

Y
Your Name 已提交
160 161
//EditAPI 编辑接口
func EditAPI(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
162 163 164

	apiID := httpRequest.PostFormValue("apiID")
	apiName := httpRequest.PostFormValue("apiName")
Y
Your Name 已提交
165
	alias := httpRequest.PostFormValue("alias")
E
eoLinker API Management 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
	requestURL := httpRequest.PostFormValue("requestURL")
	targetURL := httpRequest.PostFormValue("targetURL")
	requestMethod := httpRequest.PostFormValue("requestMethod")
	protocol := httpRequest.PostFormValue("protocol")
	balanceName := httpRequest.PostFormValue("balanceName")
	targetMethod := httpRequest.PostFormValue("targetMethod")
	isFollow := httpRequest.PostFormValue("isFollow")
	timeout := httpRequest.PostFormValue("timeout")
	retryCount := httpRequest.PostFormValue("retryCount")
	groupID := httpRequest.PostFormValue("groupID")
	projectID := httpRequest.PostFormValue("projectID")
	alertValve := httpRequest.PostFormValue("alertValve")
	managerID := httpRequest.PostFormValue("managerID")
Y
Your Name 已提交
179 180 181
	linkApis := httpRequest.PostFormValue("linkApis")
	staticResponse := httpRequest.PostFormValue("staticResponse")
	responseDataType := httpRequest.PostFormValue("responseDataType")
Y
Your Name 已提交
182 183
	userID := goku_handler.UserIDFromRequest(httpRequest)

E
eoLinker API Management 已提交
184 185 186 187 188 189 190 191 192 193
	if apiName == "" {
		controller.WriteError(httpResponse, "190002", "api", "[ERROR]Illegal apiName!", nil)
		return

	}
	aID, err := strconv.Atoi(apiID)
	if err != nil {
		controller.WriteError(httpResponse, "190001", "api", "[ERROR]Illegal apiID!", nil)
		return
	}
Y
Your Name 已提交
194 195 196 197
	if responseDataType != "origin" && responseDataType != "json" && responseDataType != "xml" {
		controller.WriteError(httpResponse, "190013", "api", "[ERROR]Illegal responseDataType!", err)
		return
	}
E
eoLinker API Management 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

	if isFollow != "true" && isFollow != "false" && isFollow != "" {
		controller.WriteError(httpResponse, "190008", "api", "[ERROR]Illegal isFollow!", nil)
		return

	}
	if isFollow == "" {
		isFollow = "false"
	}
	t, err := strconv.Atoi(timeout)
	if err != nil && timeout != "" {
		controller.WriteError(httpResponse, "190010", "api", "[ERROR]Illegal timeout!", nil)
		return

	}
	if t < 1 {
		controller.WriteError(httpResponse, "190010", "api", "[ERROR]Illegal timeout!", nil)
		return

	}
	count, err := strconv.Atoi(retryCount)
	if err != nil && retryCount != "" {
		controller.WriteError(httpResponse, "190011", "api", "[ERROR]Illegal retryCount!", nil)
		return

	}
	gID, err := strconv.Atoi(groupID)
	if err != nil {
		controller.WriteError(httpResponse, "190015", "api", "[ERROR]Illegal groupID!", nil)
		return

	}
	pjID, err := strconv.Atoi(projectID)
	if err != nil {
		controller.WriteError(httpResponse, "190016", "api", "[ERROR]Illegal projectID!", nil)
		return

	}
	apiValve, err := strconv.Atoi(alertValve)
	if err != nil && alertValve != "" {
		controller.WriteError(httpResponse, "190017", "api", "[ERROR]Illegal alertValve!", nil)
		return

	}
	mgID, err := strconv.Atoi(managerID)
	if (err != nil && managerID != "") || mgID < -1 {
		controller.WriteError(httpResponse, "190018", "api", "[ERROR]Illegal managerID!", nil)
		return

	}
	if managerID == "" {
		mgID = userID
	}
Y
Your Name 已提交
251 252 253 254 255
	if api.CheckAliasIsExist(aID, alias) {
		errInfo := "[ERROR]duplicate alias!"
		controller.WriteError(httpResponse, "190020", "api", errInfo, errors.New(errInfo))
		return
	}
E
eoLinker API Management 已提交
256

Y
Your Name 已提交
257
	flag, err := api.EditAPI(apiName, alias, requestURL, targetURL, requestMethod, targetMethod, isFollow, linkApis, staticResponse, responseDataType, balanceName, protocol, pjID, gID, t, count, apiValve, aID, mgID, userID)
E
eoLinker API Management 已提交
258 259 260 261 262 263 264 265 266 267 268 269
	if !flag {

		controller.WriteError(httpResponse, "190000", "api", "[ERROR]apiID does not exist!", err)
		return

	}

	controller.WriteResultInfo(httpResponse, "api", "", nil)

	return
}

Y
Your Name 已提交
270 271
//GetAPIInfo 获取接口信息
func GetAPIInfo(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
272 273 274 275 276 277 278 279 280

	apiID := httpRequest.PostFormValue("apiID")

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

	}
Y
Your Name 已提交
281
	flag, result, err := api.GetAPIInfo(aID)
E
eoLinker API Management 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	if !flag {
		controller.WriteError(httpResponse, "190000", "api", "[ERROR]The api does not exist!", err)
		return

	}
	controller.WriteResultInfo(httpResponse, "api", "apiInfo", result)

	return
}

// GetAPIIDList 获取接口ID列表
func GetAPIIDList(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	httpRequest.ParseForm()
	projectID := httpRequest.Form.Get("projectID")
	groupID := httpRequest.Form.Get("groupID")
	keyword := httpRequest.Form.Get("keyword")
	condition := httpRequest.Form.Get("condition")
	idsStr := httpRequest.Form.Get("ids")

	pjID, e := strconv.Atoi(projectID)
	if e != nil {
		controller.WriteError(httpResponse, "190016", "api", "[ERROR]Illegal projectID!", e)
		return
	}
	gID, e := strconv.Atoi(groupID)
	if e != nil {
		if groupID != "" {
			controller.WriteError(httpResponse, "190015", "api", "[ERROR]Illegal groupID!", e)
			return
		}
		gID = -1
	}
	op, e := strconv.Atoi(condition)
	if e != nil {
		if condition != "" {
			controller.WriteError(httpResponse, "190019", "api", "[ERROR]Illegal condition!", e)
			return
		}
	}
	ids := make([]int, 0)
	json.Unmarshal([]byte(idsStr), &ids)

Y
Your Name 已提交
325 326 327 328 329
	_, result, err := api.GetAPIIDList(pjID, gID, keyword, op, ids)
	if err != nil {
		controller.WriteError(httpResponse, "190020", "api", "[ERROR]db error!", err)
		return
	}
E
eoLinker API Management 已提交
330 331 332 333 334 335 336 337
	// controller.WriteResultInfo(httpResponse, "api", "apiList", result)
	controller.WriteResultInfoWithPage(httpResponse, "api", "apiIDList", result, &controller.PageInfo{
		ItemNum:  len(result),
		TotalNum: len(result),
	})
	return
}

Y
Your Name 已提交
338 339
//GetAPIList 获取接口列表
func GetAPIList(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

	httpRequest.ParseForm()
	projectID := httpRequest.Form.Get("projectID")
	groupID := httpRequest.Form.Get("groupID")
	keyword := httpRequest.Form.Get("keyword")
	condition := httpRequest.Form.Get("condition")
	idsStr := httpRequest.Form.Get("ids")
	page := httpRequest.Form.Get("page")
	pageSize := httpRequest.Form.Get("pageSize")

	p, e := strconv.Atoi(page)
	if e != nil {
		p = 1
	}
	pSize, e := strconv.Atoi(pageSize)
	if e != nil {
		pSize = 15
	}

	pjID, e := strconv.Atoi(projectID)
	if e != nil {
		controller.WriteError(httpResponse, "190016", "api", "[ERROR]Illegal projectID!", e)
		return
	}
	gID, e := strconv.Atoi(groupID)
	if e != nil {
		if groupID != "" {
			controller.WriteError(httpResponse, "190015", "api", "[ERROR]Illegal groupID!", e)
			return
		}
		gID = -1
	}
	op, e := strconv.Atoi(condition)
	if e != nil {
		if condition != "" {
			controller.WriteError(httpResponse, "190019", "api", "[ERROR]Illegal condition!", e)
			return
		}
	}
	result := make([]map[string]interface{}, 0)
	ids := make([]int, 0)
	json.Unmarshal([]byte(idsStr), &ids)

Y
Your Name 已提交
383 384 385 386 387
	_, result, count, err := api.GetAPIList(pjID, gID, keyword, op, p, pSize, ids)
	if err != nil {
		controller.WriteError(httpResponse, "190019", "api", "[Error]db error", err)
		return
	}
E
eoLinker API Management 已提交
388 389 390 391 392 393 394 395 396 397
	// controller.WriteResultInfo(httpResponse, "api", "apiList", result)
	controller.WriteResultInfoWithPage(httpResponse, "api", "apiList", result, &controller.PageInfo{
		ItemNum:  len(result),
		TotalNum: count,
		Page:     p,
		PageSize: pSize,
	})
	return
}

Y
Your Name 已提交
398 399
// BatchEditAPIGroup 批量修改接口分组
func BatchEditAPIGroup(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
400 401 402 403 404 405 406 407

	apiIDList := httpRequest.PostFormValue("apiIDList")
	groupID := httpRequest.PostFormValue("groupID")
	gID, err := strconv.Atoi(groupID)
	if err != nil {
		controller.WriteError(httpResponse, "190015", "api", "[ERROR]Illegal groupID!", nil)
		return
	}
Y
Your Name 已提交
408
	flag, result, err := api.BatchEditAPIGroup(strings.Split(apiIDList, ","), gID)
E
eoLinker API Management 已提交
409 410 411 412 413 414 415 416 417
	if !flag {
		controller.WriteError(httpResponse, "190015", "api", result, err)
		return
	}
	controller.WriteResultInfo(httpResponse, "api", "", nil)

	return
}

Y
Your Name 已提交
418 419
//BatchDeleteAPI 批量删除接口
func BatchDeleteAPI(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
420 421 422

	apiIDList := httpRequest.PostFormValue("apiIDList")

Y
Your Name 已提交
423
	flag, result, err := api.BatchDeleteAPI(apiIDList)
E
eoLinker API Management 已提交
424 425 426 427 428 429 430 431 432 433 434
	if !flag {

		controller.WriteError(httpResponse, "190000", "api", result, err)
		return

	}

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

Y
Your Name 已提交
435
//CopyAPI 复制接口
Y
Your Name 已提交
436
func CopyAPI(httpResponse http.ResponseWriter, httpRequest *http.Request) {
E
eoLinker API Management 已提交
437 438

	apiID := httpRequest.PostFormValue("apiID")
Y
Your Name 已提交
439
	alisa := httpRequest.PostFormValue("alisa")
E
eoLinker API Management 已提交
440 441 442 443 444 445 446 447 448 449
	apiName := httpRequest.PostFormValue("apiName")
	requestURL := httpRequest.PostFormValue("requestURL")
	targetURL := httpRequest.PostFormValue("targetURL")
	requestMethod := httpRequest.PostFormValue("requestMethod")
	protocol := httpRequest.PostFormValue("protocol")
	balanceName := httpRequest.PostFormValue("balanceName")
	targetMethod := httpRequest.PostFormValue("targetMethod")
	isFollow := httpRequest.PostFormValue("isFollow")
	groupID := httpRequest.PostFormValue("groupID")
	projectID := httpRequest.PostFormValue("projectID")
Y
Your Name 已提交
450
	userID := goku_handler.UserIDFromRequest(httpRequest)
E
eoLinker API Management 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	if apiName == "" {
		controller.WriteError(httpResponse, "190002", "api", "[ERROR]Illegal apiName!", nil)
		return

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

	if isFollow != "true" && isFollow != "false" && isFollow != "" {
		controller.WriteError(httpResponse, "190008", "api", "[ERROR]Illegal isFollow!", nil)
		return

	}
	if isFollow == "" {
		isFollow = "false"
	}
	gID, err := strconv.Atoi(groupID)
	if err != nil {
		controller.WriteError(httpResponse, "190015", "api", "[ERROR]Illegal groupID!", nil)
		return

	}
	pjID, err := strconv.Atoi(projectID)
	if err != nil {
		controller.WriteError(httpResponse, "190016", "api", "[ERROR]Illegal projectID!", nil)
		return

	}
Y
Your Name 已提交
482 483 484 485
	if !utils.ValidateURL(requestURL) {
		controller.WriteError(httpResponse, "190021", "api", "[ERROR]Illegal requestURL!", nil)
		return
	}
Y
Your Name 已提交
486
	flag, apiInfo, err := api.GetAPIInfo(aID)
E
eoLinker API Management 已提交
487 488 489 490
	if !flag {
		controller.WriteError(httpResponse, "190000", "api", "[ERROR]apiID does not exist!", nil)
		return
	}
Y
Your Name 已提交
491 492 493 494
	if apiInfo.APIType == 1 && !utils.ValidateURL(targetURL) {
		controller.WriteError(httpResponse, "190022", "api", "[ERROR]Illegal targetURL!", nil)
		return
	}
Y
Your Name 已提交
495
	linkApis, _ := json.Marshal(apiInfo.LinkAPIs)
Y
Your Name 已提交
496
	flag, id, err := api.AddAPI(apiName, alisa, requestURL, targetURL, requestMethod, targetMethod, isFollow, string(linkApis), apiInfo.StaticResponse, apiInfo.ResponseDataType, balanceName, protocol, pjID, gID, apiInfo.Timeout, apiInfo.RetryConut, apiInfo.Valve, apiInfo.ManagerID, userID, apiInfo.APIType)
E
eoLinker API Management 已提交
497 498 499 500 501 502 503 504 505
	if !flag {
		controller.WriteError(httpResponse, "190000", "api", "[ERROR]Fail to add api!", err)
		return

	}

	controller.WriteResultInfo(httpResponse, "api", "apiID", id)
	return
}