diff --git a/etcd_handlers.go b/etcd_handlers.go index 5c61c750e..f21721638 100644 --- a/etcd_handlers.go +++ b/etcd_handlers.go @@ -105,7 +105,7 @@ func CreateHttpHandler(w http.ResponseWriter, req *http.Request) error { ExpireTime: expireTime, } - return dispatch(command, w, req, true) + return dispatchEtcdCommand(command, w, req) } @@ -140,7 +140,7 @@ func UpdateHttpHandler(w http.ResponseWriter, req *http.Request) error { ExpireTime: expireTime, } - return dispatch(command, w, req, true) + return dispatchEtcdCommand(command, w, req) } else { // update with test var prevIndex uint64 @@ -161,7 +161,7 @@ func UpdateHttpHandler(w http.ResponseWriter, req *http.Request) error { PrevIndex: prevIndex, } - return dispatch(command, w, req, true) + return dispatchEtcdCommand(command, w, req) } } @@ -179,37 +179,12 @@ func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) error { command.Recursive = true } - return dispatch(command, w, req, true) + return dispatchEtcdCommand(command, w, req) } // Dispatch the command to leader -func dispatch(c Command, w http.ResponseWriter, req *http.Request, etcd bool) error { - if r.State() == raft.Leader { - if body, err := r.Do(c); err != nil { - return err - } else { - if body == nil { - return etcdErr.NewError(300, "Empty result from raft") - } else { - body, _ := body.([]byte) - w.WriteHeader(http.StatusOK) - w.Write(body) - return nil - } - } - - } else { - leader := r.Leader() - // current no leader - if leader == "" { - return etcdErr.NewError(300, "") - } - - redirect(leader, etcd, w, req) - - return nil - } - return etcdErr.NewError(300, "") +func dispatchEtcdCommand(c Command, w http.ResponseWriter, req *http.Request) error { + return dispatch(c, w, req, nameToEtcdURL) } //-------------------------------------- @@ -294,7 +269,8 @@ func GetHttpHandler(w http.ResponseWriter, req *http.Request) error { if req.FormValue("consistent") == "true" { if r.State() != raft.Leader { leader := r.Leader() - redirect(leader, true, w, req) + url, _ := nameToEtcdURL(leader) + redirect(url, w, req) return nil } } diff --git a/raft_handlers.go b/raft_handlers.go index 4fff0780d..6c95efd3f 100644 --- a/raft_handlers.go +++ b/raft_handlers.go @@ -104,7 +104,7 @@ func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error { if err := decodeJsonRequest(req, command); err == nil { debugf("Receive Join Request from %s", command.Name) - return dispatch(command, w, req, false) + return dispatchRaftCommand(command, w, req) } else { w.WriteHeader(http.StatusInternalServerError) return nil @@ -125,8 +125,7 @@ func RemoveHttpHandler(w http.ResponseWriter, req *http.Request) { debugf("[recv] Remove Request [%s]", command.Name) - dispatch(command, w, req, false) - + dispatchRaftCommand(command, w, req) } // Response to the name request @@ -142,3 +141,7 @@ func RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(r.version)) } + +func dispatchRaftCommand(c Command, w http.ResponseWriter, req *http.Request) error { + return dispatch(c, w, req, nameToRaftURL) +} diff --git a/util.go b/util.go index 530b77ced..5d64e1fac 100644 --- a/util.go +++ b/util.go @@ -65,17 +65,40 @@ func startWebInterface() { // HTTP Utilities //-------------------------------------- -func redirect(node string, etcd bool, w http.ResponseWriter, req *http.Request) { - var url string +func dispatch(c Command, w http.ResponseWriter, req *http.Request, toURL func(name string) (string, bool)) error { + if r.State() == raft.Leader { + if body, err := r.Do(c); err != nil { + return err + } else { + if body == nil { + return etcdErr.NewError(300, "Empty result from raft") + } else { + body, _ := body.([]byte) + w.WriteHeader(http.StatusOK) + w.Write(body) + return nil + } + } + + } else { + leader := r.Leader() + // current no leader + if leader == "" { + return etcdErr.NewError(300, "") + } + url, _ := toURL(leader) + + redirect(url, w, req) + + return nil + } + return etcdErr.NewError(300, "") +} + +func redirect(hostname string, w http.ResponseWriter, req *http.Request) { path := req.URL.Path - if etcd { - etcdAddr, _ := nameToEtcdURL(node) - url = etcdAddr + path - } else { - raftAddr, _ := nameToRaftURL(node) - url = raftAddr + path - } + url := hostname + path debugf("Redirect to %s", url)