Go Web开发之Revel - 插件
2013-01-08 13:19 Danny.tian 阅读(1978) 评论(0) 收藏 举报插件被注册到应用程序的hook上面和请求生命周期事件离.
一个插件就像下面的接口 (每一个事件都将被通知):
type Plugin interface {
// Server启动时被call (每一次代码重新加载).
OnAppStart()
// 路由器完成配置后被call.
OnRoutesLoaded(router *Router)
// 每一次request之前被call.
BeforeRequest(c *Controller)
// 每一次request之后被call.(除了panics)
AfterRequest(c *Controller)
// 当一个panic退出一个action时被call,并有一个recovered值
OnException(c *Controller, err interface{})
// 每次request后(无论是不是panic),Result已经被应用后被call
Finally(c *Controller)
}
定义一个你自己的插件, 声明一个嵌入rev.EmptyPlugin的类型并重写你想要的方法然后注册 rev.RegisterPlugin.
type DbPlugin struct {
rev.EmptyPlugin
}
func (p DbPlugin) OnAppStart() {
...
}
func init() {
rev.RegisterPlugin(DbPlugin{})
}
Revel将调用提供给RegisterPlugin的单例的全部方法,所以请确保方法是线程安全的.
开发区域
- 添加更多的插件可以处理的东西: 完整的请求, 渲染模板, 一个最终的调用不管是否有panic.
- 提供一个正式的地方调用 RegisterPlugin.
浙公网安备 33010602011771号