This commit is contained in:
Denis Zheleztsov 2017-02-15 19:13:12 +03:00
parent c9a6a5c629
commit f2bc4457e0
2 changed files with 43 additions and 0 deletions

View File

@ -50,6 +50,17 @@ func (zk ZooNode) LS(w http.ResponseWriter, r *http.Request) {
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 ...
func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) {
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/get{path:[a-z0-9-_/.:]+}", zk.GET)
r.HandleFunc("/v1/rmr{path:[a-z0-9-_/.:]+}", zk.RM)
http.Handle("/", r)

View File

@ -92,3 +92,34 @@ func (z ZooNode) GetNode(path string) Get {
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")
}