get_handler.go 645 B

1234567891011121314151617181920212223242526272829
  1. package v2
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "github.com/gorilla/mux"
  7. )
  8. // getHandler retrieves the current leader.
  9. func (h *handler) getHandler(w http.ResponseWriter, req *http.Request) {
  10. vars := mux.Vars(req)
  11. // Proxy the request to the lock service.
  12. url := fmt.Sprintf("%s/mod/v2/lock/%s?field=value", h.addr, vars["key"])
  13. resp, err := h.client.Get(url)
  14. if err != nil {
  15. http.Error(w, "read leader error: " + err.Error(), http.StatusInternalServerError)
  16. return
  17. }
  18. defer resp.Body.Close()
  19. if resp.StatusCode != http.StatusOK {
  20. w.Write([]byte("get leader error: "))
  21. }
  22. w.WriteHeader(resp.StatusCode)
  23. io.Copy(w, resp.Body)
  24. }