* New method: PATCH

* Fix return path string in rmr

* Version bump
This commit is contained in:
Denis Zheleztsov 2017-05-04 18:45:29 +03:00
parent 8a575eac18
commit d14ad459dd
3 changed files with 49 additions and 8 deletions

View File

@ -14,6 +14,8 @@ Zookeeper HTTP rest API
- [Get node data](#get-node-data) - [Get node data](#get-node-data)
- [Errors](#errors) - [Errors](#errors)
- [Create node recursive](#create-node-recursive) - [Create node recursive](#create-node-recursive)
- [Create node children](#create-node-children)
- [Errors](#errors)
- [Update node](#update-node) - [Update node](#update-node)
- [Errors](#errors) - [Errors](#errors)
- [Delete node recursive](#delete-node-recursive) - [Delete node recursive](#delete-node-recursive)
@ -22,7 +24,6 @@ Zookeeper HTTP rest API
- [Binary](#binary) - [Binary](#binary)
- [Docker build](#docker-build) - [Docker build](#docker-build)
- [Binary file](#binary-file) - [Binary file](#binary-file)
- [Docker image](#docker-image)
- [AUTHORS](#authors) - [AUTHORS](#authors)
- [LICENSE](#license) - [LICENSE](#license)
@ -122,6 +123,25 @@ curl -XPUT http://127.0.0.1:8889/v1/up/two/three/four -d '{"four": "json"}'
/zoorest/two/three/four /zoorest/two/three/four
``` ```
### Create node children
Method: **PATCH**
Location: **/v1/up**
Return string with created children path
```
curl -XPATCH http://127.0.0.1:8889/v1/up/one/test -d 'test'
/one/test
```
#### Errors
```
curl -XPATCH http://127.0.0.1:8889/v1/up/six/test -d '{"six": "json"}'
zk: node does not exist
```
### Update node ### Update node
Method: **POST** Method: **POST**

View File

@ -90,6 +90,8 @@ func (zk ZooNode) UP(w http.ResponseWriter, r *http.Request) {
go func() { ch <- zk.CreateNode(path, content) }() go func() { ch <- zk.CreateNode(path, content) }()
} else if r.Method == "POST" { } else if r.Method == "POST" {
go func() { ch <- zk.UpdateNode(path, content) }() go func() { ch <- zk.UpdateNode(path, content) }()
} else if r.Method == "PATCH" {
go func() { ch <- zk.CreateChild(path, content) }()
} else { } else {
e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ") e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ")
w.WriteHeader(500) w.WriteHeader(500)
@ -115,8 +117,6 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
path := vars["path"] path := vars["path"]
var rmPath string
rmPath = strings.Join([]string{zk.Path, path}, "")
if path == "/" { if path == "/" {
e := "Skiping root path" e := "Skiping root path"
w.WriteHeader(500) w.WriteHeader(500)
@ -124,6 +124,9 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) {
return return
} }
var rmPath string
rmPath = strings.Join([]string{zk.Path, path}, "")
if strings.Contains(rmPath, "//") { if strings.Contains(rmPath, "//") {
rmPath = strings.Replace(rmPath, "//", "/", 1) rmPath = strings.Replace(rmPath, "//", "/", 1)
} }
@ -171,10 +174,10 @@ func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) {
func Serve(listen string, zk ZooNode) { func Serve(listen string, zk ZooNode) {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/v1/ls{path:[a-z0-9-_/.:]+}", zk.LS) r.HandleFunc("/v1/ls{path:[A-Za-z0-9-_/.:]+}", zk.LS)
r.HandleFunc("/v1/get{path:[a-z0-9-_/.:]+}", zk.GET) r.HandleFunc("/v1/get{path:[A-Za-z0-9-_/.:]+}", zk.GET)
r.HandleFunc("/v1/rmr{path:[a-z0-9-_/.:]+}", zk.RM) r.HandleFunc("/v1/rmr{path:[A-Za-z0-9-_/.:]+}", zk.RM)
r.HandleFunc("/v1/up{path:[a-z0-9-_/.:]+}", zk.UP) r.HandleFunc("/v1/up{path:[A-Za-z0-9-_/.:]+}", zk.UP)
http.Handle("/", r) http.Handle("/", r)

View File

@ -134,7 +134,7 @@ func (z ZooNode) CreateNode(path string, content []byte) string {
return z.UpdateNode(path, content) return z.UpdateNode(path, content)
} }
// UpdateNode ... // UpdateNode update existing node
func (z ZooNode) UpdateNode(path string, content []byte) string { func (z ZooNode) UpdateNode(path string, content []byte) string {
upPath := strings.Join([]string{z.Path, path}, "") upPath := strings.Join([]string{z.Path, path}, "")
if strings.Contains(upPath, "//") { if strings.Contains(upPath, "//") {
@ -152,6 +152,24 @@ func (z ZooNode) UpdateNode(path string, content []byte) string {
return path return path
} }
// CreateChild create child in /node/path
func (z ZooNode) CreateChild(path string, content []byte) string {
crPath := strings.Join([]string{z.Path, path}, "")
if strings.Contains(crPath, "//") {
crPath = strings.Replace(crPath, "//", "/", 1)
}
if crPath == "/" {
return "Not updating root path"
}
_, err := z.Conn.Create(crPath, content, 0, zk.WorldACL(zk.PermAll))
if err != nil {
return err.Error()
}
return path
}
//EnsureZooPath create zookeeper path //EnsureZooPath create zookeeper path
func (z ZooNode) EnsureZooPath(path string) (string, error) { func (z ZooNode) EnsureZooPath(path string) (string, error) {
flag := int32(0) flag := int32(0)