forked from soyking/e3w
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
79 lines (67 loc) · 1.55 KB
/
key.go
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
package routers
import (
"github.com/gin-gonic/gin"
"github.com/soyking/e3ch"
)
type Node struct {
Key string `json:"key"`
Value string `json:"value"`
IsDir bool `json:"is_dir"`
}
func parseNode(node *client.Node) *Node {
return &Node{
Key: string(node.Key),
Value: string(node.Value),
IsDir: node.IsDir,
}
}
func getKeyHandler(c *gin.Context, client *client.EtcdHRCHYClient) (interface{}, error) {
_, list := c.GetQuery("list")
key := c.Param("key")
if list {
nodes, err := client.List(key)
if err != nil {
return nil, err
}
realNodes := []*Node{}
for _, node := range nodes {
realNodes = append(realNodes, parseNode(node))
}
return realNodes, nil
} else {
node, err := client.Get(key)
if err != nil {
return nil, err
}
return parseNode(node), nil
}
}
type postRequest struct {
Value string `json:"value"`
}
func postKeyHandler(c *gin.Context, client *client.EtcdHRCHYClient) (interface{}, error) {
_, dir := c.GetQuery("dir")
key := c.Param("key")
if dir {
return nil, client.CreateDir(key)
} else {
r := new(postRequest)
err := parseBody(c, r)
if err != nil {
return nil, err
}
return nil, client.Create(key, r.Value)
}
}
func putKeyHandler(c *gin.Context, client *client.EtcdHRCHYClient) (interface{}, error) {
key := c.Param("key")
r := new(postRequest)
err := parseBody(c, r)
if err != nil {
return nil, err
}
return nil, client.Put(key, r.Value)
}
func delKeyHandler(c *gin.Context, client *client.EtcdHRCHYClient) (interface{}, error) {
return nil, client.Delete(c.Param("key"))
}