Is gocraft/web compatible with standard http.Handlers?
lonelycode opened this issue · comments
Martin Buhr commented
Ive been trying to integrate and use middleware provided by go libraries that make use of the http.Handler pattern, but for the life of me can't figure out how to get those to work with GoCraft on a subrouter.
Any pointers would be appreciated :-)
Matt Crawford commented
@lonelycode - Here's an example that may get you started using a RedirectHandler from the http package.
package main
import (
"github.com/gocraft/web"
"net/http"
)
// Context is required for gocraft web router.
type Context struct{}
func main() {
router := web.New(Context{})
router.Get("/", func(w web.ResponseWriter, r *web.Request) {
w.Write([]byte("index page"))
})
redir := http.RedirectHandler("https://www.google.com", http.StatusTemporaryRedirect)
sub := router.Subrouter(Context{}, "/redirect")
sub.Get("/google", func(w web.ResponseWriter, r *web.Request) {
redir.ServeHTTP(w, r.Request)
})
sub.Get("/google2", func(w web.ResponseWriter, r *web.Request) {
http.Redirect(w, r.Request, "https://www.google.com", http.StatusTemporaryRedirect)
})
panic(http.ListenAndServe("localhost:9090", router))
}