etcd/server/v1/get_key_handler.go
Yicheng Qin 89d3df242c chore(server): handle HEAD for key space efficiently
Implement HEAD in server/v1 and server/v2 functions to avoid
time wasting on JSON marhsal.
2014-04-07 18:55:07 -07:00

32 lines
614 B
Go

package v1
import (
"encoding/json"
"net/http"
"github.com/coreos/etcd/third_party/github.com/gorilla/mux"
)
// Retrieves the value for a given key.
func GetKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
vars := mux.Vars(req)
key := "/" + vars["key"]
// Retrieve the key from the store.
event, err := s.Store().Get(key, false, false)
if err != nil {
return err
}
w.WriteHeader(http.StatusOK)
if req.Method == "HEAD" {
return nil
}
// Convert event to a response and write to client.
b, _ := json.Marshal(event.Response(s.Store().Index()))
w.Write(b)
return nil
}