get_key_handler.go 539 B

123456789101112131415161718192021222324252627
  1. package v1
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "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, s.CommitIndex(), s.Term())
  13. if err != nil {
  14. return err
  15. }
  16. // Convert event to a response and write to client.
  17. b, _ := json.Marshal(event.Response())
  18. w.WriteHeader(http.StatusOK)
  19. w.Write(b)
  20. return nil
  21. }