BackendDao.go 3.6 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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
package dao

import (
	"goku-ce-1.0/dao/database"	
	"goku-ce-1.0/utils"
	"encoding/json"
)

// 获取环境列表
func GetBackendList(gatewayID int) (bool,[]*utils.BackendInfo){
	db := database.GetConnection()
	rows,err := db.Query(`SELECT eo_gateway_backend.backendID,eo_gateway_backend.backendName,eo_gateway_backend.backendURI FROM eo_gateway_backend WHERE eo_gateway_backend.gatewayID = ? ORDER BY eo_gateway_backend.backendID DESC;`,gatewayID)
	
	backendList := make([]*utils.BackendInfo,0)
	flag := true
	if err != nil {
		flag = false
	}
	num :=0
	//延时关闭Rows
	defer rows.Close()
	//获取记录列
	if _, err = rows.Columns(); err != nil {
    	return false,backendList
	} else {
		for rows.Next(){
			var backend utils.BackendInfo

			err:= rows.Scan(&backend.BackendID,&backend.BackendName,&backend.BackendURI)
			if err!=nil{
				flag = false
				break
			}
			backendList = append(backendList,&backend)
			num +=1
		}
	}
	if num == 0{
		flag =false
	}
	
	return flag,backendList
}

// 添加环境
func AddBackend(gatewayID int,backendName ,backendURI string) (bool,int){
	db := database.GetConnection()
	stmt,err := db.Prepare(`INSERT INTO eo_gateway_backend (eo_gateway_backend.backendName,eo_gateway_backend.backendURI,eo_gateway_backend.gatewayID) VALUES (?,?,?);`)
	defer stmt.Close()
	if err != nil {
		return false,0
	} 
	
	res, err := stmt.Exec(backendName,backendURI,gatewayID)
	if err != nil {
		return false,0
	} else{
		id, _ := res.LastInsertId()
		return true,int(id)
	}

}

func DeleteBackend(gatewayID,backendID int) bool{
	db := database.GetConnection()
	stmt,err := db.Prepare(`DELETE FROM eo_gateway_backend WHERE eo_gateway_backend.backendID = ? AND eo_gateway_backend.gatewayID = ?;`)
	defer stmt.Close()
	if err != nil {
		return false
	} 
	
	res, err := stmt.Exec(backendID,gatewayID)
	if err != nil {
		return false
	} else{
		if rowAffect,_:=res.RowsAffected(); rowAffect > 0{
			return true
		}else{
			return false
		}
	}
}

func EditBackend(backendID,gatewayID int,backendName,backendURI,gatewayHashKey string) bool{
	db := database.GetConnection()
	Tx,_ := db.Begin()
	stmt,err := Tx.Prepare(`UPDATE eo_gateway_backend SET eo_gateway_backend.backendName = ?,eo_gateway_backend.backendURI = ? WHERE eo_gateway_backend.backendID = ?;`)
	if err != nil {
		Tx.Rollback()
		return false
	} 
	_, err = stmt.Exec(backendName,backendURI,backendID)
	if err != nil {
		Tx.Rollback()
		return false
	} else{
		Tx.Exec("UPDATE eo_gateway_api SET backendURI = ? WHERE backendID = ?",backendURI,backendID)
		Tx.Exec("UPDATE eo_gateway_api_cache SET path = ? WHERE backendID = ?",backendURI,backendID)
		
		redisConn,err := utils.GetRedisConnection()
		defer redisConn.Close()
		if err != nil{
			Tx.Rollback()
			return false
		}
		var queryJson utils.QueryJson
		var operationData utils.OperationData
		operationData.GatewayID = gatewayID
		operationData.GatewayHashKey = gatewayHashKey
		queryJson.OperationType = "backend"
		queryJson.Operation = "update"
		queryJson.Data = operationData
		redisString,_ := json.Marshal(queryJson)
		_, err = redisConn.Do("rpush", "gatewayQueue", string(redisString[:]))  
		if err != nil{
			Tx.Rollback()
			return false
		}
		Tx.Commit()
		return true
	}
}

// 获取网关信息
func GetBackendInfo(backendID int) (bool,utils.BackendInfo){
	db := database.GetConnection()
	var backendInfo utils.BackendInfo
	err := db.QueryRow("SELECT eo_gateway_backend.backendID,eo_gateway_backend.backendName,eo_gateway_backend.backendURI FROM eo_gateway_backend WHERE eo_gateway_backend.backendID = ?;",backendID).Scan(&backendInfo.BackendID,&backendInfo.BackendName,&backendInfo.BackendURI)
	if err != nil {
		return false,backendInfo
	} else{
		return true,backendInfo
	}
}