get_handler.go 2.2 KB

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