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