From 000e51f17eba47c3502ee8f717f53f1fae3d0bbd Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期三, 13 十一月 2019 19:11:35 +0800
Subject: [PATCH] new files

---
 controllers/cluster.go |  138 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 138 insertions(+), 0 deletions(-)

diff --git a/controllers/cluster.go b/controllers/cluster.go
index 2d32936..873901e 100644
--- a/controllers/cluster.go
+++ b/controllers/cluster.go
@@ -1 +1,139 @@
 package controllers
+
+import (
+	"basic.com/dbapi.git"
+	"fmt"
+	"github.com/gin-gonic/gin"
+	"math/rand"
+	"time"
+	"webserver/extend/code"
+	"webserver/extend/util"
+)
+
+type ClusterController struct {
+
+}
+
+type Cluster struct {
+	ClusterId string `json:"cluster_id"`
+	ClusterName string `json:"cluster_name"`
+	Password string `json:"password"`
+	VirtualIp string `json:"virtual_ip"`
+}
+
+type Node struct {
+	Id string `json:"id"`
+	ClusterId string `json:"cluster_id"`
+	NodeName string `json:"node_name"`
+	NodeId string `json:"node_id"`
+	NodeIp string `json:"node_ip"`
+	CreateTime string `json:"create_time"`
+	IsAlive int
+}
+
+type ClusterVo struct {
+	ClusterInfo Cluster `json:"clusterInfo"`
+	Nodes []Node `json:"nodes"`
+}
+
+type ClusterCreateVo struct {
+	Password string `json:"password"`
+	ClusterName string `json:"clusterName"`
+	ClusterId string `json:"clusterId"`
+}
+
+type ClusterSearchVo struct {
+	Password string `json:"password"`
+	SearchNum string `json:"searchNum"`
+}
+
+type ClusterJoinVo struct {
+	ClusterId string `json:"clusterId"`
+	Password string `json:"password"`
+	NodeIps []string `json:"nodeIps"`
+}
+
+
+func (cc ClusterController) Create(c *gin.Context) {
+	var clusterVo ClusterCreateVo
+	err := c.BindJSON(&clusterVo)
+	if err !=nil || clusterVo.Password == "" || clusterVo.ClusterName == "" {
+		util.ResponseFormat(c,code.RequestParamError, "鍙傛暟鏈夎")
+		return
+	}
+	var api dbapi.ClusterApi
+	b, d := api.Create(clusterVo.ClusterName, clusterVo.Password)
+	if b {
+		util.ResponseFormat(c,code.Success, d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"鍒涘缓澶辫触")
+	}
+}
+
+
+func (cc ClusterController) RandomPwd(c *gin.Context) {
+	pwd := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
+	util.ResponseFormat(c,code.Success,pwd)
+}
+
+
+func (cc ClusterController) Search(c *gin.Context) {
+	var csv ClusterSearchVo
+	err := c.BindJSON(&csv)
+	if err !=nil || csv.Password == ""{
+		util.ResponseFormat(c,code.RequestParamError, "鍙傛暟鏈夎")
+		return
+	}
+	var api dbapi.ClusterApi
+	b,d := api.Search(csv.SearchNum, csv.Password)
+	if b {
+		util.ResponseFormat(c,code.Success, d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"鎼滅储澶辫触")
+	}
+}
+
+
+func (cc ClusterController) GetSearchNodes(c *gin.Context) {
+	var api dbapi.ClusterApi
+	b, d := api.GetSearchNodes()
+	if b {
+		util.ResponseFormat(c,code.Success,d)
+	} else {
+		util.ResponseFormat(c,code.ClusterNodesEmpty,[]interface{}{})
+	}
+}
+
+
+func (cc ClusterController) StopSearching(c *gin.Context) {
+	searchNum := c.Query("searchNum")
+	if searchNum == "" {
+		util.ResponseFormat(c,code.RequestParamError, "鍙傛暟鏈夎")
+		return
+	}
+	var api dbapi.ClusterApi
+	b, d := api.StopSearching(searchNum)
+	if b {
+		util.ResponseFormat(c,code.Success, d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"鍋滄澶辫触")
+	}
+}
+
+
+func (cc ClusterController) JoinCluster(c *gin.Context) {
+	var arg ClusterJoinVo
+	err := c.BindJSON(&arg)
+	if err!=nil || arg.ClusterId == "" || arg.Password == "" || len(arg.NodeIps) == 0{
+		util.ResponseFormat(c,code.RequestParamError, "鍙傛暟鏈夎")
+		return
+	}
+	var api dbapi.ClusterApi
+	paramBody := util.Struct2Map(arg)
+	b, d := api.JoinCluster(paramBody)
+	if b {
+		util.ResponseFormat(c,code.Success,d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"鍔犲叆澶辫触")
+	}
+}

--
Gitblit v1.8.0