Skip to content

Instantly share code, notes, and snippets.

@tomyhero
Created June 23, 2015 05:55
Show Gist options
  • Save tomyhero/ebbdeea14046905e729c to your computer and use it in GitHub Desktop.
Save tomyhero/ebbdeea14046905e729c to your computer and use it in GitHub Desktop.
how to check 404 before execute ServeHTTP with goji
package main
import (
"fmt"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"net/http"
)
func main() {
goji.Get("/hello/:name", hello)
goji.Use(goji.DefaultMux.Router)
goji.Use(Check)
goji.Serve()
}
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func Check(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
pattern := web.GetMatch(*c).Pattern
if pattern != nil {
fmt.Println("ok")
} else {
fmt.Println("404")
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment