48cefc67 by astaxie

improve performance change reflect to interface

1 parent 079a4113
...@@ -55,6 +55,8 @@ type ControllerInterface interface { ...@@ -55,6 +55,8 @@ type ControllerInterface interface {
55 Options() 55 Options()
56 Finish() 56 Finish()
57 Render() error 57 Render() error
58 XsrfToken() string
59 CheckXsrfCookie() bool
58 } 60 }
59 61
60 func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) { 62 func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) {
......
...@@ -667,50 +667,45 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -667,50 +667,45 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
667 667
668 //Invoke the request handler 668 //Invoke the request handler
669 vc := reflect.New(runrouter) 669 vc := reflect.New(runrouter)
670 execController, ok := vc.Interface().(ControllerInterface)
671 if !ok {
672 panic("controller is not ControllerInterface")
673 }
670 674
671 //call the controller init function 675 //call the controller init function
672 method := vc.MethodByName("Init") 676 execController.Init(context, runrouter.Name(), runMethod, vc.Interface())
673 in := make([]reflect.Value, 4)
674 in[0] = reflect.ValueOf(context)
675 in[1] = reflect.ValueOf(runrouter.Name())
676 in[2] = reflect.ValueOf(runMethod)
677 in[3] = reflect.ValueOf(vc.Interface())
678 method.Call(in)
679 677
680 //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf 678 //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
681 if EnableXSRF { 679 if EnableXSRF {
682 in = make([]reflect.Value, 0) 680 execController.XsrfToken()
683 method = vc.MethodByName("XsrfToken")
684 method.Call(in)
685 if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" || 681 if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
686 (r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) { 682 (r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
687 method = vc.MethodByName("CheckXsrfCookie") 683 execController.CheckXsrfCookie()
688 method.Call(in)
689 } 684 }
690 } 685 }
691 686
692 //call prepare function 687 //call prepare function
693 in = make([]reflect.Value, 0) 688 execController.Prepare()
694 method = vc.MethodByName("Prepare")
695 method.Call(in)
696 689
697 if !w.started { 690 if !w.started {
698 //exec main logic 691 //exec main logic
699 method = vc.MethodByName(runMethod) 692 in := make([]reflect.Value, 0)
693 method := vc.MethodByName(runMethod)
700 method.Call(in) 694 method.Call(in)
701 695
702 //render template 696 //render template
703 if !w.started && !context.Input.IsWebsocket() { 697 if !w.started && !context.Input.IsWebsocket() {
704 if AutoRender { 698 if AutoRender {
705 method = vc.MethodByName("Render") 699 if err := execController.Render(); err != nil {
706 callMethodWithError(method, in) 700 panic(err)
701 }
702
707 } 703 }
708 } 704 }
709 } 705 }
710 706
711 // finish all runrouter. release resource 707 // finish all runrouter. release resource
712 method = vc.MethodByName("Finish") 708 execController.Finish()
713 method.Call(in)
714 709
715 //execute middleware filters 710 //execute middleware filters
716 if do_filter(AfterExec) { 711 if do_filter(AfterExec) {
...@@ -805,15 +800,3 @@ func (w *responseWriter) WriteHeader(code int) { ...@@ -805,15 +800,3 @@ func (w *responseWriter) WriteHeader(code int) {
805 w.started = true 800 w.started = true
806 w.writer.WriteHeader(code) 801 w.writer.WriteHeader(code)
807 } 802 }
808
809 // call method and panic with error if error is in result params
810 func callMethodWithError(method reflect.Value, params []reflect.Value) {
811 results := method.Call(params)
812 if len(results) > 0 {
813 for _, result := range results {
814 if result.Type() == errorType && !result.IsNil() {
815 panic(result.Interface().(error))
816 }
817 }
818 }
819 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!