Compare commits

..

No commits in common. "master" and "v0.3" have entirely different histories.
master ... v0.3

7 changed files with 19 additions and 199 deletions

View File

@ -2,7 +2,6 @@ language: go
go:
- 1.6
- 1.7
- 1.8
# tests script
script:

View File

@ -2,6 +2,7 @@
[![Build Status](https://travis-ci.org/Difrex/zoorest.svg?branch=master)](https://travis-ci.org/Difrex/zoorest)
Zookeeper HTTP rest API
<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->
**Table of Contents**
@ -23,38 +24,24 @@ Zookeeper HTTP rest API
- [Binary](#binary)
- [Docker build](#docker-build)
- [Binary file](#binary-file)
- [Docker image](#docker-image)
- [Get docker image](#get-docker-image)
- [AUTHORS](#authors)
- [LICENSE](#license)
- [DONATE](#donate)
<!-- markdown-toc end -->
## Usage
```
Usage of ./zoorest:
-listen string
Address to listen (default "127.0.0.1:8889")
-mc
Enable memcached support
-mchosts string
Memcached servers. Comma separated (default "127.0.0.1:11211")
-mcprefix string
Memcached key prefix (default "zoorest")
-path string
Zk root path (default "/")
-zk string
Zk servers. Comma separated (default "127.0.0.1:2181")
```
NOTE: If memcached support is enabled zkStat metrics will not be returned.
Tupical usage scheme:
[![tupical usage](img/usage.png)](img/usage.png)
## API
### List node childrens
@ -249,18 +236,6 @@ cd zoorest
Image will be tagged as zoorest:latest
## Get docker image
Pull image
```
docker pull lessmore/zoorest
```
And run it
```
/usr/bin/docker run -d -p 8889:8889 --name zoorest lessmore/zoorest:latest --zk zoo1:2181,zoo2:2181,zoo3:2181 --path /zoorest/jail --listen 0.0.0.0:8889 -mc -mchosts mc1:11211,mc2:11211,mc3:11211
```
# AUTHORS
Denis Zheleztsov <difrex.punk@gmail.com>
@ -268,8 +243,3 @@ Denis Zheleztsov <difrex.punk@gmail.com>
# LICENSE
GPLv3 see [LICENSE](LICENSE)
# DONATE
BTC 1JCmZQdESKPCrcjUxDRNgt5HaSgUEWZ8pV<br>
DASH XxEWcJgfiAav1gxTtVLGXqE5T66uMA7te7

View File

@ -16,7 +16,7 @@ ENTRYPOINT go build -ldflags "-linkmode external -extldflags -static" && mv zoor
EOF
# Build builder
docker build --no-cache -t zoorest_builder -f Dockerfile.builder .
docker build -t zoorest_builder -f Dockerfile.builder .
# Build bin
docker run -v $(pwd)/out:/out zoorest_builder

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

30
main.go
View File

@ -10,9 +10,6 @@ var (
zk string
listen string
path string
mc bool
mcHosts string
mcPrefix string
)
// init ...
@ -20,39 +17,24 @@ func init() {
flag.StringVar(&zk, "zk", "127.0.0.1:2181", "Zk servers. Comma separated")
flag.StringVar(&listen, "listen", "127.0.0.1:8889", "Address to listen")
flag.StringVar(&path, "path", "/", "Zk root path")
flag.BoolVar(&mc, "mc", false, "Enable memcached support")
flag.StringVar(&mcHosts, "mchosts", "127.0.0.1:11211", "Memcached servers. Comma separated")
flag.StringVar(&mcPrefix, "mcprefix", "zoorest", "Memcached key prefix")
flag.Parse()
}
// main ...
func main() {
var z rest.Zk
hosts := getSlice(zk, ",")
hosts := strings.Split(zk, ",")
z.Hosts = hosts
conn, err := z.InitConnection()
if err != nil {
panic(err)
}
var zoo rest.ZooNode
zoo.Path = path
zoo.Conn = conn
zoo.Zoo = z
var MC rest.MC
MC.Hosts = getSlice(mcHosts, ",")
MC.Prefix = mcPrefix
MC.Enabled = mc
MC.Client = MC.InitConnection()
zoo.MC = MC
zoo := rest.ZooNode{
path,
conn,
z,
}
rest.Serve(listen, zoo)
}
// getSlice returm slice
func getSlice(s string, delimeter string) []string {
return strings.Split(s, ",")
}

View File

@ -1,69 +0,0 @@
package rest
import (
"crypto/md5"
"encoding/base64"
"github.com/bradfitz/gomemcache/memcache"
"strings"
)
// Memcached connection settings
type MC struct {
Enabled bool `json:"enabled"`
Hosts []string `json:"hosts"`
Prefix string `json:"prefix"`
Client *memcache.Client
}
// InitConnection to memcached cluster
func (mc MC) InitConnection() *memcache.Client {
client := memcache.New(mc.Hosts...)
return client
}
// StoreToCache save data to memecache
func (mc MC) StoreToCache(key string, data []byte) error {
prefixed_key := strings.Join([]string{mc.Prefix, key}, "_")
var item memcache.Item
// item.Key = hashKey(prefixed_key)
item.Key = hashKey(prefixed_key)
item.Value = data
// store
err := mc.Client.Set(&item)
return err
}
// GetFromCache get node data from memcache
func (mc MC) GetFromCache(key string) ([]byte, error) {
var data *memcache.Item
prefixed_key := strings.Join([]string{mc.Prefix, key}, "_")
data, err := mc.Client.Get(hashKey(prefixed_key))
if err != nil {
return []byte(""), err
}
return data.Value, err
}
// DeleteFromCache delete key from memcache
func (mc MC) DeleteFromCache(key string) error {
prefixed_key := strings.Join([]string{mc.Prefix, key}, "_")
err := mc.Client.Delete(hashKey(prefixed_key))
return err
}
// hashKey return base64 encoded md5sum of prefixed_key
func hashKey(key string) string {
h := md5.New()
sum := h.Sum([]byte(key))
b64 := base64.StdEncoding.EncodeToString(sum)
return b64
}

View File

@ -3,7 +3,6 @@ package rest
import (
"github.com/samuel/go-zookeeper/zk"
"log"
"sort"
"strings"
"time"
)
@ -13,7 +12,6 @@ type ZooNode struct {
Path string
Conn *zk.Conn
Zoo Zk
MC MC
}
//Zk Zookeeper connection settings
@ -27,7 +25,7 @@ type Zk struct {
func (conf Zk) InitConnection() (*zk.Conn, error) {
conn, _, err := zk.Connect(conf.Hosts, time.Second)
if err != nil {
log.Fatal("[ERROR] ", err)
log.Panic("[ERROR] ", err)
}
return conn, err
@ -45,23 +43,10 @@ func (z ZooNode) GetChildrens(path string) Ls {
lsPath = strings.Replace(lsPath, "//", "/", 1)
}
log.Print("V1 LS: ", lsPath)
log.Print("ls: ", lsPath)
var l Ls
l.State = "OK"
l.Path = path
// if z.MC.Enabled {
// data, err := z.MC.GetFromCache(lsPath)
// if err != nil {
// log.Print("V1 LS ERROR: ", err.Error())
// } else {
// log.Print("We are get it from memecache!")
// childrens := strings.Split(string(data), ",")
// l.Childrens = childrens
// return l
// }
// }
childrens, zkStat, err := z.Conn.Children(lsPath)
if err != nil {
@ -70,25 +55,15 @@ func (z ZooNode) GetChildrens(path string) Ls {
return l
}
// // Store to cache
// if z.MC.Enabled {
// err := z.MC.StoreToCache(lsPath, []byte(strings.Join(childrens, ",")))
// if err != nil {
// log.Print("V1 LS: ", err.Error())
// }
// }
// Sort childrens alphabeticaly
sort.Strings(childrens)
l.Error = ""
l.Childrens = childrens
l.Path = path
l.ZkStat = zkStat
return l
}
// GetNode data
// GetNode ...
func (z ZooNode) GetNode(path string) Get {
var getPath string
getPath = strings.Join([]string{z.Path, path}, "")
@ -100,21 +75,10 @@ func (z ZooNode) GetNode(path string) Get {
getPath = strings.Replace(getPath, "//", "/", 1)
}
log.Print("V1 GET: ", getPath)
log.Print("get: ", getPath)
var g Get
g.State = "OK"
g.Path = path
// Get data from memcached
if z.MC.Enabled {
if data, err := z.MC.GetFromCache(getPath); err != nil {
log.Print("[mc ERROR] ", err.Error())
} else {
g.Data = data
return g
}
}
data, zkStat, err := z.Conn.Get(getPath)
if err != nil {
@ -123,16 +87,9 @@ func (z ZooNode) GetNode(path string) Get {
return g
}
// Store to cache
if z.MC.Enabled {
err := z.MC.StoreToCache(getPath, data)
if err != nil {
log.Print("[mc ERROR] ", err.Error())
}
}
g.Error = ""
g.Data = data
g.Path = path
g.ZkStat = zkStat
return g
@ -155,15 +112,8 @@ func (z ZooNode) RMR(path string) {
err = z.Conn.Delete(path, -1)
if err != nil {
log.Print("[zk ERROR] ", err)
} else {
if z.MC.Enabled {
err := z.MC.DeleteFromCache(path)
if err != nil {
log.Print("[mc ERROR] ", err.Error())
}
}
log.Print("[WARNING] ", path, " deleted")
}
}
// CreateNode ...
@ -199,12 +149,6 @@ func (z ZooNode) UpdateNode(path string, content []byte) string {
return err.Error()
}
if z.MC.Enabled {
if err := z.MC.StoreToCache(upPath, content); err != nil {
log.Print("[mc ERROR] ", err.Error())
}
}
return path
}
@ -223,12 +167,6 @@ func (z ZooNode) CreateChild(path string, content []byte) string {
return err.Error()
}
if z.MC.Enabled {
if err := z.MC.StoreToCache(crPath, content); err != nil {
log.Print("[mc ERROR] ", err.Error())
}
}
return path
}