Skip to content

Commit

Permalink
real node, hide rootKey
Browse files Browse the repository at this point in the history
  • Loading branch information
soyking committed Oct 23, 2016
1 parent 1a838fe commit 8fc6390
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
"golang.org/x/net/context"
"strings"
)

const (
Expand Down Expand Up @@ -63,6 +64,8 @@ func (clt *EtcdHRCHYClient) isDir(value []byte) bool {
}

func (clt *EtcdHRCHYClient) createNode(kv *mvccpb.KeyValue) *Node {
// remove rootKey prefix
kv.Key = []byte(strings.TrimPrefix(string(kv.Key), clt.rootKey+"/"))
return &Node{
KeyValue: kv,
IsDir: clt.isDir(kv.Value),
Expand Down
38 changes: 36 additions & 2 deletions routers/key.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
package routers

import (
"encoding/base64"
"github.com/gin-gonic/gin"
"github.com/soyking/e3w/client"
)

type Node struct {
Key string `json:"key"`
Value string `json:"value"`
IsDir bool `json:"is_dir"`
}

func base64Decode(src string) (string, error) {
dst, err := base64.StdEncoding.DecodeString(src)
return string(dst), err
}

func parseNode(node *client.Node) *Node {
return &Node{
Key: string(node.Key),
Value: string(node.Value),
IsDir: node.IsDir,
}
}

func getKeyHandler(client *client.EtcdHRCHYClient) respHandler {
return func(c *gin.Context) (interface{}, error) {
_, list := c.GetQuery("list")
key := c.Param("key")

if list {
return client.List(key)
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 {
return client.Get(key)
node, err := client.Get(key)
if err != nil {
return nil, err
}

return parseNode(node), nil
}
}
}
Expand Down

0 comments on commit 8fc6390

Please sign in to comment.