get_handler.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package v2
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. etcdErr "github.com/coreos/etcd/error"
  8. "github.com/coreos/etcd/log"
  9. "github.com/coreos/etcd/store"
  10. "github.com/coreos/go-raft"
  11. "github.com/gorilla/mux"
  12. )
  13. func GetHandler(w http.ResponseWriter, req *http.Request, s Server) error {
  14. var err error
  15. var event *store.Event
  16. vars := mux.Vars(req)
  17. key := "/" + vars["key"]
  18. // Help client to redirect the request to the current leader
  19. if req.FormValue("consistent") == "true" && s.State() != raft.Leader {
  20. leader := s.Leader()
  21. hostname, _ := s.PeerURL(leader)
  22. url := hostname + req.URL.Path
  23. log.Debugf("Redirect consistent get to %s", url)
  24. http.Redirect(w, req, url, http.StatusTemporaryRedirect)
  25. return nil
  26. }
  27. recursive := (req.FormValue("recursive") == "true")
  28. sorted := (req.FormValue("sorted") == "true")
  29. if req.FormValue("wait") == "true" { // watch
  30. // Create a command to watch from a given index (default 0).
  31. var sinceIndex uint64 = 0
  32. waitIndex := req.FormValue("waitIndex")
  33. if waitIndex != "" {
  34. sinceIndex, err = strconv.ParseUint(string(req.FormValue("waitIndex")), 10, 64)
  35. if err != nil {
  36. return etcdErr.NewError(etcdErr.EcodeIndexNaN, "Watch From Index", s.Store().Index())
  37. }
  38. }
  39. // Start the watcher on the store.
  40. eventChan, err := s.Store().Watch(key, recursive, sinceIndex)
  41. if err != nil {
  42. return etcdErr.NewError(500, key, s.Store().Index())
  43. }
  44. cn, _ := w.(http.CloseNotifier)
  45. closeChan := cn.CloseNotify()
  46. select {
  47. case <-closeChan:
  48. return nil
  49. case event = <-eventChan:
  50. }
  51. } else { //get
  52. // Retrieve the key from the store.
  53. event, err = s.Store().Get(key, recursive, sorted)
  54. if err != nil {
  55. return err
  56. }
  57. }
  58. w.Header().Set("Content-Type", "application/json")
  59. w.Header().Add("X-Etcd-Index", fmt.Sprint(s.Store().Index()))
  60. w.Header().Add("X-Raft-Index", fmt.Sprint(s.CommitIndex()))
  61. w.Header().Add("X-Raft-Term", fmt.Sprint(s.Term()))
  62. w.WriteHeader(http.StatusOK)
  63. b, _ := json.Marshal(event)
  64. w.Write(b)
  65. return nil
  66. }