google app engine - Appengine with Go: Is there a http.Handle prehook or something similar? -
suppose have following init function routing requests.
func init() { http.handlefunc("/user", handler1) http.handlefunc("/user/profile", handler2) http.handlefunc("/user/post", handler3) .... .... }
all of these require have user's profile.
i know can
func handler1(w http.responsewriter, r *http.request) { getuserdata() //actual handler code ... ... }
but, there way can data without putting function call in every handler? go want in first place?
you have 2 options.
- you can inplement
http.handler
interface - you wrap
http.handlerfunc
wrapper handlefunc.
since looks want simple i'll illustrate wrapper
func prehook(f http.handlerfunc) http.handlerfunc { return func(w http.responsewriter, r *http.request) { getuserdata() f(w, r) } } func init() { // use getuserdata() call before handler http.handlefunc("/user", prehook(handler1)) // don't use getuserdata call before handler http.handlefunc("/user/profile", handler2) }
Comments
Post a Comment