mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 11:50:55 +00:00
improve performance change reflect to interface
This commit is contained in:
parent
079a41136e
commit
48cefc6767
@ -55,6 +55,8 @@ type ControllerInterface interface {
|
||||
Options()
|
||||
Finish()
|
||||
Render() error
|
||||
XsrfToken() string
|
||||
CheckXsrfCookie() bool
|
||||
}
|
||||
|
||||
func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) {
|
||||
|
47
router.go
47
router.go
@ -667,50 +667,45 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
|
||||
//Invoke the request handler
|
||||
vc := reflect.New(runrouter)
|
||||
execController, ok := vc.Interface().(ControllerInterface)
|
||||
if !ok {
|
||||
panic("controller is not ControllerInterface")
|
||||
}
|
||||
|
||||
//call the controller init function
|
||||
method := vc.MethodByName("Init")
|
||||
in := make([]reflect.Value, 4)
|
||||
in[0] = reflect.ValueOf(context)
|
||||
in[1] = reflect.ValueOf(runrouter.Name())
|
||||
in[2] = reflect.ValueOf(runMethod)
|
||||
in[3] = reflect.ValueOf(vc.Interface())
|
||||
method.Call(in)
|
||||
execController.Init(context, runrouter.Name(), runMethod, vc.Interface())
|
||||
|
||||
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
||||
if EnableXSRF {
|
||||
in = make([]reflect.Value, 0)
|
||||
method = vc.MethodByName("XsrfToken")
|
||||
method.Call(in)
|
||||
execController.XsrfToken()
|
||||
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
||||
(r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
|
||||
method = vc.MethodByName("CheckXsrfCookie")
|
||||
method.Call(in)
|
||||
execController.CheckXsrfCookie()
|
||||
}
|
||||
}
|
||||
|
||||
//call prepare function
|
||||
in = make([]reflect.Value, 0)
|
||||
method = vc.MethodByName("Prepare")
|
||||
method.Call(in)
|
||||
execController.Prepare()
|
||||
|
||||
if !w.started {
|
||||
//exec main logic
|
||||
method = vc.MethodByName(runMethod)
|
||||
in := make([]reflect.Value, 0)
|
||||
method := vc.MethodByName(runMethod)
|
||||
method.Call(in)
|
||||
|
||||
//render template
|
||||
if !w.started && !context.Input.IsWebsocket() {
|
||||
if AutoRender {
|
||||
method = vc.MethodByName("Render")
|
||||
callMethodWithError(method, in)
|
||||
if err := execController.Render(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// finish all runrouter. release resource
|
||||
method = vc.MethodByName("Finish")
|
||||
method.Call(in)
|
||||
execController.Finish()
|
||||
|
||||
//execute middleware filters
|
||||
if do_filter(AfterExec) {
|
||||
@ -805,15 +800,3 @@ func (w *responseWriter) WriteHeader(code int) {
|
||||
w.started = true
|
||||
w.writer.WriteHeader(code)
|
||||
}
|
||||
|
||||
// call method and panic with error if error is in result params
|
||||
func callMethodWithError(method reflect.Value, params []reflect.Value) {
|
||||
results := method.Call(params)
|
||||
if len(results) > 0 {
|
||||
for _, result := range results {
|
||||
if result.Type() == errorType && !result.IsNil() {
|
||||
panic(result.Interface().(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user