redirect.go 981 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package render
  5. import (
  6. "fmt"
  7. "net/http"
  8. )
  9. // Redirect contains the http request reference and redirects status code and location.
  10. type Redirect struct {
  11. Code int
  12. Request *http.Request
  13. Location string
  14. }
  15. // Render (Redirect) redirects the http request to new location and writes redirect response.
  16. func (r Redirect) Render(w http.ResponseWriter) error {
  17. // todo(thinkerou): go1.6 not support StatusPermanentRedirect(308)
  18. // when we upgrade go version we can use http.StatusPermanentRedirect
  19. if (r.Code < 300 || r.Code > 308) && r.Code != 201 {
  20. panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
  21. }
  22. http.Redirect(w, r.Request, r.Location, r.Code)
  23. return nil
  24. }
  25. // WriteContentType (Redirect) don't write any ContentType.
  26. func (r Redirect) WriteContentType(http.ResponseWriter) {}