liuxiaolong
2020-06-08 2a4041f16c6588921c87df93927e9076c2cc309d
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
251
252
253
254
255
256
257
258
259
260
261
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"`
    VirtualIp string `json:"virtualIp"`
}
 
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"`
}
 
type ClusterDb struct {
    ClusterId   string `json:"clusterId"`
    ClusterName string `json:"clusterName"`
    Nodes []NodeDb     `json:"nodes"`
}
type NodeDb 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"`
    IsDelete bool `json:"isDelete"`
}
 
// @Security ApiKeyAuth
// @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, clusterVo.VirtualIp)
    if b {
        util.ResponseFormat(c,code.Success, d)
    } else {
        util.ResponseFormat(c,code.ComError,"创建失败")
    }
}
 
// @Security ApiKeyAuth
// @Summary 生成集群6位随机密码
// @Description 生成集群6位随机密码
// @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/randomPwd [get]
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)
}
 
// @Security ApiKeyAuth
// @Summary 查询本地集群
// @Description 查询本地集群
// @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/findCluster [get]
func (cc ClusterController) FindCluster(c *gin.Context) {
    var clusterApi dbapi.ClusterApi
    b, d := clusterApi.FindCluster()
    if b {
        util.ResponseFormat(c,code.Success, d)
    } else {
        util.ResponseFormat(c,code.ComError,"集群查询失败")
    }
}
 
// @Security ApiKeyAuth
// @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,"搜索失败")
    }
}
 
// @Security ApiKeyAuth
// @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{}{})
    }
}
 
// @Security ApiKeyAuth
// @Summary 通过searchNum停止搜索
// @Description 通过searchNum停止搜索
// @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/stopSearching [post]
func (cc ClusterController) StopSearching(c *gin.Context) {
    var api dbapi.ClusterApi
    b, d := api.StopSearching("")
    if b {
        util.ResponseFormat(c,code.Success, d)
    } else {
        util.ResponseFormat(c,code.ComError,"停止失败")
    }
}
 
// @Security ApiKeyAuth
// @Summary 加入集群
// @Description 加入集群
// @Accept json
// @Produce json
// @Tags cluster
// @Param  clusterArg body controllers.ClusterJoinVo 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,"加入失败")
    }
}
 
// @Security ApiKeyAuth
// @Summary 保存集群名称
// @Description 保存集群名称
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags cluster
// @Param clusterName formData string true "集群名称"
// @Param virtualIp formData string false "虚拟ip"
// @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/updateClusterName [post]
func (cc ClusterController) UpdateClusterName(c *gin.Context) {
    clusterName := c.PostForm("clusterName")
    if clusterName == "" {
        util.ResponseFormat(c,code.RequestParamError, "参数有误")
        return
    }
    virtualIp := c.PostForm("virtualIp")
    var api dbapi.ClusterApi
    b,_ := api.UpdateClusterName(clusterName,virtualIp)
    if b {
        util.ResponseFormat(c,code.UpdateSuccess,"更新成功")
    } else {
        util.ResponseFormat(c,code.ComError, "更新失败")
    }
}
 
// @Security ApiKeyAuth
// @Summary 退出集群
// @Description 退出集群
// @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/leave [post]
func (cc ClusterController) Leave(c *gin.Context) {
    var api dbapi.ClusterApi
    if b,_:= api.Leave();b {
        util.ResponseFormat(c,code.Success,"退出成功")
    } else {
        util.ResponseFormat(c,code.ComError,"退出失败")
    }
}