web icon indicating copy to clipboard operation
web copied to clipboard

is there any way add hooks called before every request?

Open zx9597446 opened this issue 11 years ago • 2 comments

would be nice has this feature, some application need to check cookie every request, or doing some global settings.

zx9597446 avatar Aug 14 '13 03:08 zx9597446

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
}

shivakumargn avatar Aug 14 '13 04:08 shivakumargn

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.

zx9597446 avatar Aug 14 '13 08:08 zx9597446