redirect.go 517 B

12345678910111213141516171819202122
  1. package render
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. type redirectRender struct{}
  7. func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
  8. req := data[0].(*http.Request)
  9. location := data[1].(string)
  10. WriteRedirect(w, code, req, location)
  11. return nil
  12. }
  13. func WriteRedirect(w http.ResponseWriter, code int, req *http.Request, location string) {
  14. if code < 300 || code > 308 {
  15. panic(fmt.Sprintf("Cannot redirect with status code %d", code))
  16. }
  17. http.Redirect(w, req, location, code)
  18. }