mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 17:21:02 +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()
|
Options()
|
||||||
Finish()
|
Finish()
|
||||||
Render() error
|
Render() error
|
||||||
|
XsrfToken() string
|
||||||
|
CheckXsrfCookie() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) {
|
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
|
//Invoke the request handler
|
||||||
vc := reflect.New(runrouter)
|
vc := reflect.New(runrouter)
|
||||||
|
execController, ok := vc.Interface().(ControllerInterface)
|
||||||
|
if !ok {
|
||||||
|
panic("controller is not ControllerInterface")
|
||||||
|
}
|
||||||
|
|
||||||
//call the controller init function
|
//call the controller init function
|
||||||
method := vc.MethodByName("Init")
|
execController.Init(context, runrouter.Name(), runMethod, vc.Interface())
|
||||||
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)
|
|
||||||
|
|
||||||
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
||||||
if EnableXSRF {
|
if EnableXSRF {
|
||||||
in = make([]reflect.Value, 0)
|
execController.XsrfToken()
|
||||||
method = vc.MethodByName("XsrfToken")
|
|
||||||
method.Call(in)
|
|
||||||
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
||||||
(r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
|
(r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
|
||||||
method = vc.MethodByName("CheckXsrfCookie")
|
execController.CheckXsrfCookie()
|
||||||
method.Call(in)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//call prepare function
|
//call prepare function
|
||||||
in = make([]reflect.Value, 0)
|
execController.Prepare()
|
||||||
method = vc.MethodByName("Prepare")
|
|
||||||
method.Call(in)
|
|
||||||
|
|
||||||
if !w.started {
|
if !w.started {
|
||||||
//exec main logic
|
//exec main logic
|
||||||
method = vc.MethodByName(runMethod)
|
in := make([]reflect.Value, 0)
|
||||||
|
method := vc.MethodByName(runMethod)
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
|
|
||||||
//render template
|
//render template
|
||||||
if !w.started && !context.Input.IsWebsocket() {
|
if !w.started && !context.Input.IsWebsocket() {
|
||||||
if AutoRender {
|
if AutoRender {
|
||||||
method = vc.MethodByName("Render")
|
if err := execController.Render(); err != nil {
|
||||||
callMethodWithError(method, in)
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// finish all runrouter. release resource
|
// finish all runrouter. release resource
|
||||||
method = vc.MethodByName("Finish")
|
execController.Finish()
|
||||||
method.Call(in)
|
|
||||||
|
|
||||||
//execute middleware filters
|
//execute middleware filters
|
||||||
if do_filter(AfterExec) {
|
if do_filter(AfterExec) {
|
||||||
@ -805,15 +800,3 @@ func (w *responseWriter) WriteHeader(code int) {
|
|||||||
w.started = true
|
w.started = true
|
||||||
w.writer.WriteHeader(code)
|
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