etcd/server/v1/get_key_handler.go

29 lines
536 B
Go
Raw Normal View History

2013-10-11 08:42:45 +04:00
package v1
import (
"encoding/json"
"net/http"
2013-10-13 01:56:43 +04:00
"github.com/gorilla/mux"
2013-10-11 08:42:45 +04:00
)
// Retrieves the value for a given key.
2013-10-13 01:56:43 +04:00
func GetKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
2013-10-11 08:42:45 +04:00
vars := mux.Vars(req)
key := "/" + vars["key"]
2013-10-11 10:07:22 +04:00
// Retrieve the key from the store.
event, err := s.Store().Get(key, false, false)
2013-10-11 08:42:45 +04:00
if err != nil {
return err
}
// Convert event to a response and write to client.
2013-12-11 23:12:39 +04:00
b, _ := json.Marshal(event.Response(s.Store().Index()))
2013-10-11 08:42:45 +04:00
w.WriteHeader(http.StatusOK)
w.Write(b)
return nil
}