get_key_handler.go 614 B

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