2 Star 1 Fork 1

极速代码/dcmp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.go 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
silenceper 提交于 2017-02-22 22:56 +08:00 . base_path support config #9
package main
import (
"strings"
"github.com/coreos/etcd/client"
"github.com/gin-gonic/gin"
"github.com/silenceper/dcmp/etcd"
"github.com/silenceper/dcmp/util"
)
type TreeNode struct {
Name string `json:"name"`
Dir bool `json:"dir"`
Toggled bool `json:"toggled"`
Path string `json:"path"`
Value string `json:"value"`
Children []*TreeNode `json:"children"`
}
func doIndex(c *gin.Context) {
c.Redirect(302, "/frontend")
}
func doKeyList(c *gin.Context) {
resp, err := etcd.GetKeyList(cfg.BasePath)
if err != nil {
util.RenderError(c, -1, err.Error())
return
}
treeNodes := formatEtcdNodes(resp.Node)
util.RenderSuccess(c, treeNodes)
}
func doKeyNew(c *gin.Context) {
key, keyExists := c.GetPostForm("key")
if !keyExists || key == "" {
util.RenderError(c, -1, "参数错误")
return
}
isDir, exists := c.GetPostForm("isDir")
if !exists || isDir == "" {
util.RenderError(c, -1, "参数错误")
return
}
opts := &client.SetOptions{}
if isDir == "yes" {
opts.Dir = true
}
resp, err := etcd.SetKey(key, "", opts)
if err != nil {
util.RenderError(c, -1, err.Error())
return
}
util.RenderSuccess(c, resp)
}
func doKeySave(c *gin.Context) {
key, keyExists := c.GetPostForm("key")
if !keyExists || key == "" {
util.RenderError(c, -1, "参数错误")
return
}
value, valueExists := c.GetPostForm("value")
if !valueExists {
util.RenderError(c, -1, "参数错误")
return
}
resp, err := etcd.UpdateKey(key, value)
if err != nil {
util.RenderError(c, -1, err.Error())
return
}
util.RenderSuccess(c, resp)
}
func doKeyDelete(c *gin.Context) {
key, keyExists := c.GetPostForm("key")
if !keyExists || key == "" {
util.RenderError(c, -1, "参数错误")
return
}
isDir, exists := c.GetPostForm("isDir")
if !exists || isDir == "" {
util.RenderError(c, -1, "参数错误")
return
}
opts := &client.DeleteOptions{}
if isDir == "yes" {
opts.Dir = true
}
resp, err := etcd.DeleteKey(key, opts)
if err != nil {
util.RenderError(c, -1, err.Error())
return
}
util.RenderSuccess(c, resp)
}
func formatEtcdNodes(node *client.Node) *TreeNode {
treeNode := &TreeNode{}
arr := strings.Split(node.Key, "/")
count := len(arr)
if count < 1 {
count = 1
}
treeNode.Name = arr[count-1]
treeNode.Path = node.Key
treeNode.Dir = node.Dir
treeNode.Value = node.Value
treeNode.Toggled = true
for _, v := range node.Nodes {
treeNode.Children = append(treeNode.Children, formatEtcdNodes(v))
}
//必须返回一个空数组供前端渲染
if treeNode.Dir && len(treeNode.Children) == 0 {
treeNode.Children = []*TreeNode{}
}
return treeNode
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/andy521/dcmp.git
git@gitee.com:andy521/dcmp.git
andy521
dcmp
dcmp
master

搜索帮助