* New method: PATCH
* Fix return path string in rmr * Version bump
This commit is contained in:
parent
8a575eac18
commit
d14ad459dd
22
README.md
22
README.md
@ -14,6 +14,8 @@ Zookeeper HTTP rest API
|
||||
- [Get node data](#get-node-data)
|
||||
- [Errors](#errors)
|
||||
- [Create node recursive](#create-node-recursive)
|
||||
- [Create node children](#create-node-children)
|
||||
- [Errors](#errors)
|
||||
- [Update node](#update-node)
|
||||
- [Errors](#errors)
|
||||
- [Delete node recursive](#delete-node-recursive)
|
||||
@ -22,7 +24,6 @@ Zookeeper HTTP rest API
|
||||
- [Binary](#binary)
|
||||
- [Docker build](#docker-build)
|
||||
- [Binary file](#binary-file)
|
||||
- [Docker image](#docker-image)
|
||||
- [AUTHORS](#authors)
|
||||
- [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
|
||||
```
|
||||
|
||||
### 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
|
||||
|
||||
Method: **POST**
|
||||
|
15
rest/api.go
15
rest/api.go
@ -90,6 +90,8 @@ func (zk ZooNode) UP(w http.ResponseWriter, r *http.Request) {
|
||||
go func() { ch <- zk.CreateNode(path, content) }()
|
||||
} else if r.Method == "POST" {
|
||||
go func() { ch <- zk.UpdateNode(path, content) }()
|
||||
} else if r.Method == "PATCH" {
|
||||
go func() { ch <- zk.CreateChild(path, content) }()
|
||||
} else {
|
||||
e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ")
|
||||
w.WriteHeader(500)
|
||||
@ -115,8 +117,6 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
path := vars["path"]
|
||||
|
||||
var rmPath string
|
||||
rmPath = strings.Join([]string{zk.Path, path}, "")
|
||||
if path == "/" {
|
||||
e := "Skiping root path"
|
||||
w.WriteHeader(500)
|
||||
@ -124,6 +124,9 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var rmPath string
|
||||
rmPath = strings.Join([]string{zk.Path, path}, "")
|
||||
|
||||
if strings.Contains(rmPath, "//") {
|
||||
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) {
|
||||
r := mux.NewRouter()
|
||||
|
||||
r.HandleFunc("/v1/ls{path:[a-z0-9-_/.:]+}", zk.LS)
|
||||
r.HandleFunc("/v1/get{path:[a-z0-9-_/.:]+}", zk.GET)
|
||||
r.HandleFunc("/v1/rmr{path:[a-z0-9-_/.:]+}", zk.RM)
|
||||
r.HandleFunc("/v1/up{path:[a-z0-9-_/.:]+}", zk.UP)
|
||||
r.HandleFunc("/v1/ls{path:[A-Za-z0-9-_/.:]+}", zk.LS)
|
||||
r.HandleFunc("/v1/get{path:[A-Za-z0-9-_/.:]+}", zk.GET)
|
||||
r.HandleFunc("/v1/rmr{path:[A-Za-z0-9-_/.:]+}", zk.RM)
|
||||
r.HandleFunc("/v1/up{path:[A-Za-z0-9-_/.:]+}", zk.UP)
|
||||
|
||||
http.Handle("/", r)
|
||||
|
||||
|
20
rest/zoo.go
20
rest/zoo.go
@ -134,7 +134,7 @@ func (z ZooNode) CreateNode(path string, content []byte) string {
|
||||
return z.UpdateNode(path, content)
|
||||
}
|
||||
|
||||
// UpdateNode ...
|
||||
// UpdateNode update existing node
|
||||
func (z ZooNode) UpdateNode(path string, content []byte) string {
|
||||
upPath := strings.Join([]string{z.Path, path}, "")
|
||||
if strings.Contains(upPath, "//") {
|
||||
@ -152,6 +152,24 @@ func (z ZooNode) UpdateNode(path string, content []byte) string {
|
||||
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
|
||||
func (z ZooNode) EnsureZooPath(path string) (string, error) {
|
||||
flag := int32(0)
|
||||
|
Loading…
Reference in New Issue
Block a user