goji ミドルウェアー内で404の判断を、ServeHTTPの手前でする方法
routerのmatch処理前のフックになるので、
冗長だけど、matchを自分ですればできるって話
- goji.DefaultMux.Router を読み込む
- web.GetMatch(*c) が動作するようになるので、それを活用
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) }