github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/server/v1/get_key_handler.go (about)

     1  package v1
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/coreos/etcd/third_party/github.com/gorilla/mux"
     8  )
     9  
    10  // Retrieves the value for a given key.
    11  func GetKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
    12  	vars := mux.Vars(req)
    13  	key := "/" + vars["key"]
    14  
    15  	// Retrieve the key from the store.
    16  	event, err := s.Store().Get(key, false, false)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	// Convert event to a response and write to client.
    22  	b, _ := json.Marshal(event.Response(s.Store().Index()))
    23  
    24  	w.WriteHeader(http.StatusOK)
    25  	w.Write(b)
    26  
    27  	return nil
    28  }