KUKU
KUKU

Reputation: 43

Beego request cycle hooks

I want to know if Beego has some hooks that I can use for every request in app to inject some functionality. For instance, BeforeControllerLoads or AfterInitRequestLoads. I did not found any of similar hooks. ORM hooks do not fit. Thanks

Upvotes: 0

Views: 270

Answers (1)

In controller you have both Prepare and Finish:

// Prepare runs after Init before request function execution.
func (c *Controller) Prepare() {}

// Finish runs after request function execution.
func (c *Controller) Finish() {}

Also via filters you can insert any hook:

beego.InsertFilter("/user/:id([0-9]+)", beego.BeforeRouter, FilterUser)

You have different execution points:

// default filter execution points
const (
    BeforeStatic = iota
    BeforeRouter
    BeforeExec
    AfterExec
    FinishRouter
)

https://github.com/astaxie/beego/blob/develop/controller.go https://github.com/astaxie/beego/blob/develop/router.go

Upvotes: 0

Related Questions