From f4e8f206a6760bdc31734dfcb1c65916b5b76311 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期四, 22 八月 2019 12:05:58 +0800
Subject: [PATCH] add cluster
---
controllers/cluster.go | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
extend/code/code.go | 1
router/router.go | 9 +++
3 files changed, 178 insertions(+), 0 deletions(-)
diff --git a/controllers/cluster.go b/controllers/cluster.go
index 2d32936..4d844ee 100644
--- a/controllers/cluster.go
+++ b/controllers/cluster.go
@@ -1 +1,169 @@
package controllers
+
+import (
+ "basic.com/dbapi.git"
+ "github.com/gin-gonic/gin"
+ "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"`
+}
+
+// @Summary 鍒涘缓闆嗙兢
+// @Description 鍒涘缓闆嗙兢
+// @Accept json
+// @Produce json
+// @Tags cluster
+// @Param clusterArg body controllers.ClusterCreateVo true "闆嗙兢鍒涘缓鍙傛暟"
+// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
+// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
+// @Router /data/api-v/cluster/create [post]
+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,"鍒涘缓澶辫触")
+ }
+}
+
+// @Summary 鎼滅储闆嗙兢
+// @Description 鎼滅储闆嗙兢
+// @Accept json
+// @Produce json
+// @Tags cluster
+// @Param searchArg body controllers.ClusterSearchVo true "闆嗙兢鎼滅储鍙傛暟"
+// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
+// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
+// @Router /data/api-v/cluster/search [post]
+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,"鎼滅储澶辫触")
+ }
+}
+
+// @Summary 閫氳繃searchNum寮傛鑾峰彇闆嗙兢鑺傜偣淇℃伅
+// @Description 閫氳繃searchNum寮傛鑾峰彇闆嗙兢鑺傜偣淇℃伅
+// @Accept json
+// @Produce json
+// @Tags cluster
+// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
+// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
+// @Router /data/api-v/cluster/getNodesByNum [get]
+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{}{})
+ }
+}
+
+// @Summary 閫氳繃searchNum鍋滄鎼滅储
+// @Description 閫氳繃searchNum鍋滄鎼滅储
+// @Produce json
+// @Tags cluster
+// @Param searchNum query string true "鎼滅储缂栧彿"
+// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
+// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
+// @Router /data/api-v/cluster/stopSearching [post]
+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,"鍋滄澶辫触")
+ }
+}
+
+
+// @Summary 鍔犲叆闆嗙兢
+// @Description 鍔犲叆闆嗙兢
+// @Accept json
+// @Produce json
+// @Tags cluster
+// @Param clusterArg body controllers.ClusterCreateVo true "闆嗙兢鍒涘缓鍙傛暟"
+// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
+// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
+// @Router /data/api-v/cluster/joinCluster [post]
+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,"鍔犲叆澶辫触")
+ }
+}
diff --git a/extend/code/code.go b/extend/code/code.go
index 0a5380d..93a4341 100644
--- a/extend/code/code.go
+++ b/extend/code/code.go
@@ -57,4 +57,5 @@
TaskStoped = &Code{http.StatusOK, false, "姝や换鍔′负鍋滅敤鐘舵�侊紝璇峰湪浠诲姟绠$悊涓紑鍚紒"}
ComError = &Code{http.StatusInternalServerError, false, ""}
+ ClusterNodesEmpty = &Code{http.StatusOK, true, "闆嗙兢鑺傜偣涓虹┖"}
)
diff --git a/router/router.go b/router/router.go
index ac00538..151d3d9 100644
--- a/router/router.go
+++ b/router/router.go
@@ -39,6 +39,7 @@
pollConfigController := new(controllers.PollConfigController)
fileController := new(controllers.FileController)
sysMenuController :=new(controllers.SysMenuController)
+ clusterController :=new(controllers.ClusterController)
sysApi := r.Group( "/data/api-u/sys")
@@ -223,6 +224,14 @@
pollCApi.GET("/getPollConfig", pollConfigController.GetPollConfig)
pollCApi.POST("/updateEnable", pollConfigController.UpdateEnable)
}
+ clusterApi := r.Group(urlPrefix+"/cluster")
+ {
+ clusterApi.POST("/create", clusterController.Create)
+ clusterApi.POST("/search", clusterController.Search)
+ clusterApi.POST("/stopSearching", clusterController.StopSearching)
+ clusterApi.GET("/getSearchNodes", clusterController.GetSearchNodes)
+ clusterApi.POST("/joinCluster", clusterController.JoinCluster)
+ }
// 鏂囦欢 涓婁紶
r.Static("static", "./static") // 闈欐�佹枃浠�
--
Gitblit v1.8.0