web
web copied to clipboard
is there any way add hooks called before every request?
would be nice has this feature, some application need to check cookie every request, or doing some global settings.
That feature is already available by the language itself in a elegant manner. No need for webgo enhancement.
// registering the handler with a wrapper
s.Get(rr.url, wrap_handler(final_handler))
// wrapper returns a function that checks cookie and then calls the final handler
func wrap_handler(handler func(http.ResponseWriter, *http.Request), wrap_flags string) func(http.ResponseWriter *http.Request) {
f := func(w http.ResponseWriter, r *http.Request) {
// process cookie here
handler(w, r)
}
return f
}
woooo, that's my 'aha' moment, recently I read about 'function adpater' but never thought it can used for this, thanks! would be better added to tour.