redirect.go 540 B

123456789101112131415161718192021222324
  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. type Redirect struct {
  10. Code int
  11. Request *http.Request
  12. Location string
  13. }
  14. func (r Redirect) Render(w http.ResponseWriter) error {
  15. if (r.Code < 300 || r.Code > 308) && r.Code != 201 {
  16. panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
  17. }
  18. http.Redirect(w, r.Request, r.Location, r.Code)
  19. return nil
  20. }