get_index_handler.go 674 B

123456789101112131415161718192021222324252627282930
  1. package v2
  2. import (
  3. "net/http"
  4. "path"
  5. "strconv"
  6. "github.com/gorilla/mux"
  7. )
  8. // getIndexHandler retrieves the current lock index.
  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. // Read all indices.
  14. resp, err := h.client.Get(keypath, true, true)
  15. if err != nil {
  16. http.Error(w, "lock children lookup error: " + err.Error(), http.StatusInternalServerError)
  17. return
  18. }
  19. // Write out the index of the last one to the response body.
  20. indices := extractResponseIndices(resp)
  21. if len(indices) > 0 {
  22. w.Write([]byte(strconv.Itoa(indices[0])))
  23. }
  24. }