util.go 607 B

1234567891011121314151617181920212223242526
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "github.com/coreos/etcd/log"
  8. )
  9. func decodeJsonRequest(req *http.Request, data interface{}) error {
  10. decoder := json.NewDecoder(req.Body)
  11. if err := decoder.Decode(&data); err != nil && err != io.EOF {
  12. log.Warnf("Malformed json request: %v", err)
  13. return fmt.Errorf("Malformed json request: %v", err)
  14. }
  15. return nil
  16. }
  17. func redirect(hostname string, w http.ResponseWriter, req *http.Request) {
  18. path := req.URL.Path
  19. url := hostname + path
  20. log.Debugf("Redirect to %s", url)
  21. http.Redirect(w, req, url, http.StatusTemporaryRedirect)
  22. }