add do_filter func to reduce duplicated code
Showing
1 changed file
with
34 additions
and
79 deletions
| ... | @@ -426,6 +426,24 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -426,6 +426,24 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 426 | context.Output.Context = context | 426 | context.Output.Context = context |
| 427 | context.Output.EnableGzip = EnableGzip | 427 | context.Output.EnableGzip = EnableGzip |
| 428 | 428 | ||
| 429 | do_filter := func(pos int) (started bool) { | ||
| 430 | if p.enableFilter { | ||
| 431 | if l, ok := p.filters[pos]; ok { | ||
| 432 | for _, filterR := range l { | ||
| 433 | if ok, p := filterR.ValidRouter(r.URL.Path); ok { | ||
| 434 | context.Input.Params = p | ||
| 435 | filterR.filterFunc(context) | ||
| 436 | if w.started { | ||
| 437 | return true | ||
| 438 | } | ||
| 439 | } | ||
| 440 | } | ||
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | return false | ||
| 445 | } | ||
| 446 | |||
| 429 | if context.Input.IsWebsocket() { | 447 | if context.Input.IsWebsocket() { |
| 430 | context.ResponseWriter = rw | 448 | context.ResponseWriter = rw |
| 431 | context.Output = beecontext.NewOutput(rw) | 449 | context.Output = beecontext.NewOutput(rw) |
| ... | @@ -436,19 +454,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -436,19 +454,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 436 | goto Admin | 454 | goto Admin |
| 437 | } | 455 | } |
| 438 | 456 | ||
| 439 | if p.enableFilter { | 457 | if do_filter(BeforeRouter) { |
| 440 | if l, ok := p.filters[BeforeRouter]; ok { | ||
| 441 | for _, filterR := range l { | ||
| 442 | if ok, p := filterR.ValidRouter(r.URL.Path); ok { | ||
| 443 | context.Input.Params = p | ||
| 444 | filterR.filterFunc(context) | ||
| 445 | if w.started { | ||
| 446 | goto Admin | 458 | goto Admin |
| 447 | } | 459 | } |
| 448 | } | ||
| 449 | } | ||
| 450 | } | ||
| 451 | } | ||
| 452 | 460 | ||
| 453 | //static file server | 461 | //static file server |
| 454 | for prefix, staticDir := range StaticDir { | 462 | for prefix, staticDir := range StaticDir { |
| ... | @@ -516,19 +524,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -516,19 +524,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 516 | context.Input.CruSession = GlobalSessions.SessionStart(w, r) | 524 | context.Input.CruSession = GlobalSessions.SessionStart(w, r) |
| 517 | } | 525 | } |
| 518 | 526 | ||
| 519 | if p.enableFilter { | 527 | if do_filter(AfterStatic) { |
| 520 | if l, ok := p.filters[AfterStatic]; ok { | ||
| 521 | for _, filterR := range l { | ||
| 522 | if ok, p := filterR.ValidRouter(r.URL.Path); ok { | ||
| 523 | context.Input.Params = p | ||
| 524 | filterR.filterFunc(context) | ||
| 525 | if w.started { | ||
| 526 | goto Admin | 528 | goto Admin |
| 527 | } | 529 | } |
| 528 | } | ||
| 529 | } | ||
| 530 | } | ||
| 531 | } | ||
| 532 | 530 | ||
| 533 | if CopyRequestBody { | 531 | if CopyRequestBody { |
| 534 | context.Input.Body() | 532 | context.Input.Body() |
| ... | @@ -592,19 +590,10 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -592,19 +590,10 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 592 | r.ParseMultipartForm(MaxMemory) | 590 | r.ParseMultipartForm(MaxMemory) |
| 593 | } | 591 | } |
| 594 | //execute middleware filters | 592 | //execute middleware filters |
| 595 | if p.enableFilter { | 593 | if do_filter(BeforeExec) { |
| 596 | if l, ok := p.filters[BeforeExec]; ok { | ||
| 597 | for _, filterR := range l { | ||
| 598 | if ok, p := filterR.ValidRouter(r.URL.Path); ok { | ||
| 599 | context.Input.Params = p | ||
| 600 | filterR.filterFunc(context) | ||
| 601 | if w.started { | ||
| 602 | goto Admin | 594 | goto Admin |
| 603 | } | 595 | } |
| 604 | } | 596 | |
| 605 | } | ||
| 606 | } | ||
| 607 | } | ||
| 608 | //Invoke the request handler | 597 | //Invoke the request handler |
| 609 | vc := reflect.New(runrouter.controllerType) | 598 | vc := reflect.New(runrouter.controllerType) |
| 610 | 599 | ||
| ... | @@ -747,20 +736,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -747,20 +736,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 747 | 736 | ||
| 748 | method = vc.MethodByName("Finish") | 737 | method = vc.MethodByName("Finish") |
| 749 | method.Call(in) | 738 | method.Call(in) |
| 739 | |||
| 750 | //execute middleware filters | 740 | //execute middleware filters |
| 751 | if p.enableFilter { | 741 | if do_filter(AfterExec) { |
| 752 | if l, ok := p.filters[AfterExec]; ok { | ||
| 753 | for _, filterR := range l { | ||
| 754 | if ok, p := filterR.ValidRouter(r.URL.Path); ok { | ||
| 755 | context.Input.Params = p | ||
| 756 | filterR.filterFunc(context) | ||
| 757 | if w.started { | ||
| 758 | goto Admin | 742 | goto Admin |
| 759 | } | 743 | } |
| 760 | } | 744 | |
| 761 | } | ||
| 762 | } | ||
| 763 | } | ||
| 764 | method = vc.MethodByName("Destructor") | 745 | method = vc.MethodByName("Destructor") |
| 765 | method.Call(in) | 746 | method.Call(in) |
| 766 | } | 747 | } |
| ... | @@ -797,20 +778,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -797,20 +778,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 797 | } | 778 | } |
| 798 | // set find | 779 | // set find |
| 799 | findrouter = true | 780 | findrouter = true |
| 781 | |||
| 800 | //execute middleware filters | 782 | //execute middleware filters |
| 801 | if p.enableFilter { | 783 | if do_filter(BeforeExec) { |
| 802 | if l, ok := p.filters[BeforeExec]; ok { | ||
| 803 | for _, filterR := range l { | ||
| 804 | if ok, p := filterR.ValidRouter(r.URL.Path); ok { | ||
| 805 | context.Input.Params = p | ||
| 806 | filterR.filterFunc(context) | ||
| 807 | if w.started { | ||
| 808 | goto Admin | 784 | goto Admin |
| 809 | } | 785 | } |
| 810 | } | 786 | |
| 811 | } | ||
| 812 | } | ||
| 813 | } | ||
| 814 | //parse params | 787 | //parse params |
| 815 | otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):] | 788 | otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):] |
| 816 | if len(otherurl) > 1 { | 789 | if len(otherurl) > 1 { |
| ... | @@ -853,22 +826,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -853,22 +826,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 853 | } | 826 | } |
| 854 | method = vc.MethodByName("Finish") | 827 | method = vc.MethodByName("Finish") |
| 855 | method.Call(in) | 828 | method.Call(in) |
| 829 | |||
| 856 | //execute middleware filters | 830 | //execute middleware filters |
| 857 | if p.enableFilter { | 831 | if do_filter(AfterExec) { |
| 858 | if l, ok := p.filters[AfterExec]; ok { | ||
| 859 | for _, filterR := range l { | ||
| 860 | if ok, p := filterR.ValidRouter(r.URL.Path); ok { | ||
| 861 | context.Input.Params = p | ||
| 862 | filterR.filterFunc(context) | ||
| 863 | if w.started { | ||
| 864 | goto Admin | 832 | goto Admin |
| 865 | } | 833 | } |
| 866 | } | 834 | |
| 867 | } | ||
| 868 | } | ||
| 869 | } | ||
| 870 | method = vc.MethodByName("Destructor") | 835 | method = vc.MethodByName("Destructor") |
| 871 | method.Call(in) | 836 | method.Call(in) |
| 837 | |||
| 872 | goto Admin | 838 | goto Admin |
| 873 | } | 839 | } |
| 874 | } | 840 | } |
| ... | @@ -883,19 +849,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -883,19 +849,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 883 | } | 849 | } |
| 884 | 850 | ||
| 885 | Admin: | 851 | Admin: |
| 886 | if p.enableFilter { | 852 | do_filter(FinishRouter) |
| 887 | if l, ok := p.filters[FinishRouter]; ok { | 853 | |
| 888 | for _, filterR := range l { | ||
| 889 | if ok, p := filterR.ValidRouter(r.URL.Path); ok { | ||
| 890 | context.Input.Params = p | ||
| 891 | filterR.filterFunc(context) | ||
| 892 | if w.started { | ||
| 893 | break | ||
| 894 | } | ||
| 895 | } | ||
| 896 | } | ||
| 897 | } | ||
| 898 | } | ||
| 899 | //admin module record QPS | 854 | //admin module record QPS |
| 900 | if EnableAdmin { | 855 | if EnableAdmin { |
| 901 | timeend := time.Since(starttime) | 856 | timeend := time.Since(starttime) | ... | ... |
-
Please register or sign in to post a comment