redirect.go 905 B

1234567891011121314151617181920212223242526272829
  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. if (r.Code < http.StatusMultipleChoices || r.Code > http.StatusPermanentRedirect) && r.Code != http.StatusCreated {
  18. panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
  19. }
  20. http.Redirect(w, r.Request, r.Location, r.Code)
  21. return nil
  22. }
  23. // WriteContentType (Redirect) don't write any ContentType.
  24. func (r Redirect) WriteContentType(http.ResponseWriter) {}