github.com/jpetazzo/etcd@v0.2.1-0.20140113055439-97f1363afac5/mod/lock/v2/get_index_handler.go (about)

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