An “interceptor” is a function that is invoked by the framework before or after an action invocation. It allows a form of Aspect Oriented Programming, which is useful for some common concerns: * Request logging * Error handling * Stats keeping
In Revel, an interceptor can take one of two forms:
InterceptorFunc
interface.
revel.Result
Interceptors are called in the order that they are added.
An interceptor can be registered to run at four points in the request lifecycle:
Interceptors typically return nil
, in which case the request continues to
be processed without interruption.
The effect of returning a non-nil
revel.Result
depends on when the interceptor
was invoked.
In all cases, any returned Result will take the place of any existing Result.
In the BEFORE case, however, that returned Result is guaranteed to be final, while in the AFTER case it is possible that a further interceptor could emit its own Result.
Here’s a simple example defining and registering a Func Interceptor.
func checkUser(c *revel.Controller) revel.Result { if user := connected(c); user == nil { c.Flash.Error("Please log in first") return c.Redirect(App.Index) } return nil } func init() { revel.InterceptFunc(checkUser, revel.BEFORE, &Hotels{}) }
A method interceptor signature may have one of these two forms:
func (c AppController) example() revel.Result func (c *AppController) example() revel.Result
Here’s the same example that operates only on the app controller.
func (c Hotels) checkUser() revel.Result { if user := connected(c); user == nil { c.Flash.Error("Please log in first") return c.Redirect(App.Index) } return nil } func init() { revel.InterceptMethod(Hotels.checkUser, revel.BEFORE) }