get_key_handler.go 684 B

12345678910111213141516171819202122232425262728293031
  1. package v1
  2. import (
  3. "encoding/json"
  4. "github.com/coreos/etcd/store"
  5. "net/http"
  6. )
  7. // Retrieves the value for a given key.
  8. func getKeyHandler(w http.ResponseWriter, req *http.Request, e *etcdServer) error {
  9. vars := mux.Vars(req)
  10. key := "/" + vars["key"]
  11. debugf("[recv] GET %s/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
  12. // Execute the command.
  13. command := &GetCommand{Key: key}
  14. event, err := command.Apply(e.raftServer.Server)
  15. if err != nil {
  16. return err
  17. }
  18. // Convert event to a response and write to client.
  19. event, _ := event.(*store.Event)
  20. response := eventToResponse(event)
  21. b, _ := json.Marshal(response)
  22. w.WriteHeader(http.StatusOK)
  23. w.Write(b)
  24. return nil
  25. }