Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Revel filter


May 15, 2021 Revel


Table of contents


Filters are middleware for the Revel framework - a separate feature that forms the request processing pipeline. They perform all the functions of the framework.

The filter type is a simple function:

type Filter func(c *Controller, filterChain []Filter)

Each filter is responsible for calling the next filter in the filter chain. Here's a default filter stack:

// Filters 是默认的全局过滤器集。
// 可以在程序初始化时设置它。
var Filters = []Filter{
    PanicFilter,             // 从恐慌中恢复,并显示一个错误页面。
    RouterFilter,            // 负责解析路由,并选择正确的控制器方法。
    FilterConfiguringFilter, // 用于添加/删除每个动作过滤的钩子。
    ParamsFilter,            // 解析参数到 Controller.Params 中。
    SessionFilter,           // 恢复和写入会话 cookie。
    FlashFilter,             // 恢复和写入 flash cookie。
    ValidationFilter,        // 恢复保存验证错误并保存新的Cookie中。
    I18nFilter,              // 解析请求语言。
    InterceptorFilter,       // 执行拦截器。
    ActionInvoker,           // 调用控制器。
}

Filter chain configuration

Global configuration

The program init() revel in revel.Filters variable to configure the filter chain (default at app/init.go

func init() {
    // Filters 是默认的全局过滤器集。
    revel.Filters = []Filter{
        PanicFilter,             // 从恐慌中恢复,并显示一个错误页面。
        RouterFilter,            // 负责解析路由,并选择正确的控制器方法。
        FilterConfiguringFilter, // 用于添加/删除每个动作过滤的钩子。
        ParamsFilter,            // 解析参数到 Controller.Params 中。
        SessionFilter,           // 恢复和写入会话 cookie。
        FlashFilter,             // 恢复和写入 flash cookie。
        ValidationFilter,        // 恢复保存验证错误并保存新的Cookie中。
        I18nFilter,              // 解析请求语言。
        InterceptorFilter,       // 执行拦截器。
        ActionInvoker,           // 调用控制器。
    }
}

Each request is executed from top to bottom along the filter chain.

Per-Action configuration

Although all requests are sent to the revel.Filters Revel also provides a 过滤器配置 developers to add, insert, and remove filters based on actions or controllers.

This feature FilterConfiguringFilter which is itself a filter.

Implement a filter

Keep the filter chain executed in turn

Filters is responsible for calling the next filter in turn to process the request in turn. This usually requires the following expression to be completed:

var MyFilter = func(c *revel.Controller, fc []revel.Filter) {
    // .. 做一些预处理 ..

    fc[0](c, fc[1:]) // 执行下一个过滤器

    // .. 做一些后期处理 ..
}

Gets the controller type

Filters accepts a parameter *Controller not the actual controller type that is called. If the filter needs access to the actual controller type, you can do this:

var MyFilter = func(c *revel.Controller, fc []revel.Filter) {
    if ac, err := c.AppController.(*MyController); err == nil {
        // 判定存在一个 *MyController 实例...
    }

    fc[0](c, fc[1:]) // 执行下一个过滤器
}

Note: This pattern often indicates that the interceptor may be an indicator of a good mechanism for implementing the required functionality.