rmr
This commit is contained in:
parent
c9a6a5c629
commit
f2bc4457e0
12
rest/api.go
12
rest/api.go
@ -50,6 +50,17 @@ func (zk ZooNode) LS(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write(data)
|
w.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RM ...
|
||||||
|
func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
path := vars["path"]
|
||||||
|
|
||||||
|
go func() { zk.RMR(path) }()
|
||||||
|
|
||||||
|
w.WriteHeader(200)
|
||||||
|
// w.Write(data)
|
||||||
|
}
|
||||||
|
|
||||||
// GET ...
|
// GET ...
|
||||||
func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) {
|
func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
@ -83,6 +94,7 @@ func Serve(listen string, zk ZooNode) {
|
|||||||
|
|
||||||
r.HandleFunc("/v1/ls{path:[a-z0-9-_/.:]+}", zk.LS)
|
r.HandleFunc("/v1/ls{path:[a-z0-9-_/.:]+}", zk.LS)
|
||||||
r.HandleFunc("/v1/get{path:[a-z0-9-_/.:]+}", zk.GET)
|
r.HandleFunc("/v1/get{path:[a-z0-9-_/.:]+}", zk.GET)
|
||||||
|
r.HandleFunc("/v1/rmr{path:[a-z0-9-_/.:]+}", zk.RM)
|
||||||
|
|
||||||
http.Handle("/", r)
|
http.Handle("/", r)
|
||||||
|
|
||||||
|
31
rest/zoo.go
31
rest/zoo.go
@ -92,3 +92,34 @@ func (z ZooNode) GetNode(path string) Get {
|
|||||||
|
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//RMR remove Zk node recursive
|
||||||
|
func (z ZooNode) RMR(path string) {
|
||||||
|
var rmPath string
|
||||||
|
rmPath = strings.Join([]string{z.Path, path}, "")
|
||||||
|
if path == "/" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(rmPath, "//") {
|
||||||
|
rmPath = strings.Replace(rmPath, "//", "/", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Print("rm: ", rmPath)
|
||||||
|
c, _, err := z.Conn.Children(rmPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Print("[zk ERROR] ", err)
|
||||||
|
}
|
||||||
|
log.Print("[WARNING] Trying delete ", path)
|
||||||
|
if len(c) > 0 {
|
||||||
|
for _, child := range c {
|
||||||
|
childPath := strings.Join([]string{rmPath, child}, "/")
|
||||||
|
z.RMR(childPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = z.Conn.Delete(path, -1)
|
||||||
|
if err != nil {
|
||||||
|
log.Print("[zk ERROR] ", err)
|
||||||
|
}
|
||||||
|
log.Print("[WARNING] ", path, " deleted")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user