save.go 1.1 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package balance

import (
	"fmt"
黄孟柱 已提交
5 6 7
	"github.com/eolinker/goku-api-gateway/common/auto-form"
	"github.com/eolinker/goku-api-gateway/console/controller"
	"github.com/eolinker/goku-api-gateway/console/module/balance"
E
eoLinker API Management 已提交
8
	"github.com/go-sql-driver/mysql"
Y
Your Name 已提交
9
	"net/http"
E
eoLinker API Management 已提交
10 11
)

Y
Your Name 已提交
12
//SaveBalance 新增负载配置
E
eoLinker API Management 已提交
13 14
func SaveBalance(w http.ResponseWriter, r *http.Request) {

Y
Your Name 已提交
15

E
eoLinker API Management 已提交
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

	if err := r.ParseForm(); err != nil {
		controller.WriteError(w, "260000", "data", "[param_check] Parse form body error | 解析form表单参数错误", err)
		return
	}
	param := new(balance.Param)
	err := auto.SetValues(r.PostForm, param)
	if err != nil {
		controller.WriteError(w, "260000", "data", fmt.Sprintf("[param_check] %s", err.Error()), err)
		return
	}

	restlt, err := balance.Save(param)

	if err != nil {
		mysqlError, ok := err.(*mysql.MySQLError)
		if ok && mysqlError.Number == 1062 {
			controller.WriteError(w, "260002", "balance", "负载名重复", err)
			return
		}
		controller.WriteError(w, "260000", "balance", restlt, err)
		return
	}

	controller.WriteResultInfo(w, "balance", "", nil)
	return
}