Created
June 23, 2015 05:55
-
-
Save tomyhero/ebbdeea14046905e729c to your computer and use it in GitHub Desktop.
how to check 404 before execute ServeHTTP with goji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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