Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is gocraft/web compatible with standard http.Handlers? #68

Open
lonelycode opened this issue Jul 22, 2016 · 1 comment
Open

Is gocraft/web compatible with standard http.Handlers? #68

lonelycode opened this issue Jul 22, 2016 · 1 comment

Comments

@lonelycode
Copy link

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 :-)

@mlctrez
Copy link
Contributor

mlctrez commented Jul 22, 2016

@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))

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants