gocraft/web

How to handle redirect

sporto opened this issue · 2 comments

Hi
Atm I am doing something like:

router.Get("/foo", (*controllers.RootContext).RedirectToDashboard)

func (c *RootContext) RedirectToDashboard(w web.ResponseWriter, req *web.Request) {
    http.Redirect(w, req.Request, "/a/dashboard", http.StatusFound)
}

Is this the best way to do it or is there a more succinct way that I am missing?

Maybe it would be nice something like:

router.Redirect("/foo", "/a/dashboard")

I would like this feature as well

You could try this:

router.Get("/foo", redirectTo("/a/dashboard"))

with

func redirectTo(to string) func(web.ResponseWriter, *web.Request) {
    return func(rw web.ResponseWriter, req *web.Request) {
       http.Redirect(rw, req.Request, to, http.StatusFound)
    }
}