get_index_handler.go 985 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package v2
  2. import (
  3. "net/http"
  4. "path"
  5. "github.com/gorilla/mux"
  6. )
  7. // getIndexHandler retrieves the current lock index.
  8. // The "field" parameter specifies to read either the lock "index" or lock "value".
  9. func (h *handler) getIndexHandler(w http.ResponseWriter, req *http.Request) {
  10. h.client.SyncCluster()
  11. vars := mux.Vars(req)
  12. keypath := path.Join(prefix, vars["key"])
  13. field := req.FormValue("field")
  14. if len(field) == 0 {
  15. field = "value"
  16. }
  17. // Read all indices.
  18. resp, err := h.client.Get(keypath, true, true)
  19. if err != nil {
  20. http.Error(w, "read lock error: " + err.Error(), http.StatusInternalServerError)
  21. return
  22. }
  23. nodes := lockNodes{resp.Node.Nodes}
  24. // Write out the requested field.
  25. if node := nodes.First(); node != nil {
  26. switch field {
  27. case "index":
  28. w.Write([]byte(path.Base(node.Key)))
  29. case "value":
  30. w.Write([]byte(node.Value))
  31. default:
  32. http.Error(w, "read lock error: invalid field: " + field, http.StatusInternalServerError)
  33. }
  34. }
  35. }