Skip to content

Instantly share code, notes, and snippets.

@itang
Last active December 25, 2015 18:09
Show Gist options
  • Select an option

  • Save itang/7018592 to your computer and use it in GitHub Desktop.

Select an option

Save itang/7018592 to your computer and use it in GitHub Desktop.
try around interceptor
package controllers
import (
"fmt"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
)
type BaseController struct {
beego.Controller
start int64
end int64
}
func (this BaseController) RenderString(s string) {
this.Ctx.WriteString(s)
}
func (this *BaseController) Init(ctx *context.Context, childName string, app interface{}) {
this.Controller.Init(ctx, childName, app)
this.start = time.Now().UnixNano()
println("init")
}
func (this *BaseController) Prepare() {
this.Controller.Prepare()
println("prepare")
}
func (this *BaseController) Finish() {
this.Controller.Finish()
this.end = time.Now().UnixNano()
println("end")
cost := (this.end - this.start) / (1000 * 1000.0)
fmt.Printf("cost time: %d ms.", cost)
println("")
// 这时候已经迟了!!!
this.Ctx.ResponseWriter.Header().Add("x-runtime", fmt.Sprintf("%d", cost))
}
func (this *BaseController) Render() error {
//this.Controller.Render()
println("render")
return nil
}
beego.Controller
start int64
end int64
}
func (this BaseController) RenderString(s string) {
this.Ctx.WriteString(s)
}
func (this *BaseController) Init(ctx *context.Context, childName string, app interface{}) {
this.Controller.Init(ctx, childName, app)
this.start = time.Now().UnixNano()
println("init")
}
func (this *BaseController) Prepare() {
this.Controller.Prepare()
println("prepare")
}
func (this *BaseController) Finish() {
this.Controller.Finish()
this.end = time.Now().UnixNano()
println("end")
cost := (this.end - this.start) / (1000 * 1000.0)
fmt.Printf("cost time: %d ms.", cost)
println("")
// 这时候已经迟了!!!
this.Ctx.ResponseWriter.Header().Add("x-runtime", cost)
}
///////////////////////////////////////////
type MainController struct {
BaseController
}
func (this *MainController) Get() {
println("before get")
time.Sleep(time.Second * 2)
this.RenderString("hello,world!")
println("after get")
}
package controllers
import (
"time"
)
type MainController struct {
BaseController
}
func (this *MainController) Get() {
println("before get")
time.Sleep(time.Second * 2)
this.RenderString("hello,world!")
println("after get")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment