support filter to get router. get runController & runMethod
Showing
2 changed files
with
37 additions
and
26 deletions
| ... | @@ -4,6 +4,7 @@ import ( | ... | @@ -4,6 +4,7 @@ import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "io/ioutil" | 5 | "io/ioutil" |
| 6 | "net/http" | 6 | "net/http" |
| 7 | "reflect" | ||
| 7 | "strconv" | 8 | "strconv" |
| 8 | "strings" | 9 | "strings" |
| 9 | 10 | ||
| ... | @@ -13,11 +14,13 @@ import ( | ... | @@ -13,11 +14,13 @@ import ( |
| 13 | // BeegoInput operates the http request header ,data ,cookie and body. | 14 | // BeegoInput operates the http request header ,data ,cookie and body. |
| 14 | // it also contains router params and current session. | 15 | // it also contains router params and current session. |
| 15 | type BeegoInput struct { | 16 | type BeegoInput struct { |
| 16 | CruSession session.SessionStore | 17 | CruSession session.SessionStore |
| 17 | Params map[string]string | 18 | Params map[string]string |
| 18 | Data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. | 19 | Data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. |
| 19 | Request *http.Request | 20 | Request *http.Request |
| 20 | RequestBody []byte | 21 | RequestBody []byte |
| 22 | RunController reflect.Type | ||
| 23 | RunMethod string | ||
| 21 | } | 24 | } |
| 22 | 25 | ||
| 23 | // NewInput return BeegoInput generated by http.Request. | 26 | // NewInput return BeegoInput generated by http.Request. | ... | ... |
| ... | @@ -626,29 +626,37 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -626,29 +626,37 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 626 | context.Input.Body() | 626 | context.Input.Body() |
| 627 | } | 627 | } |
| 628 | 628 | ||
| 629 | if context.Input.RunController != nil && context.Input.RunMethod { | ||
| 630 | findrouter = true | ||
| 631 | runMethod = context.Input.RunMethod | ||
| 632 | runrouter = context.Input.RunController | ||
| 633 | } | ||
| 634 | |||
| 629 | //first find path from the fixrouters to Improve Performance | 635 | //first find path from the fixrouters to Improve Performance |
| 630 | for _, route := range p.fixrouters { | 636 | if !findrouter { |
| 631 | n := len(requestPath) | 637 | for _, route := range p.fixrouters { |
| 632 | if requestPath == route.pattern { | 638 | n := len(requestPath) |
| 633 | runMethod = p.getRunMethod(r.Method, context, route) | 639 | if requestPath == route.pattern { |
| 634 | if runMethod != "" { | 640 | runMethod = p.getRunMethod(r.Method, context, route) |
| 635 | runrouter = route.controllerType | 641 | if runMethod != "" { |
| 636 | findrouter = true | 642 | runrouter = route.controllerType |
| 637 | break | 643 | findrouter = true |
| 644 | break | ||
| 645 | } | ||
| 638 | } | 646 | } |
| 639 | } | 647 | // pattern /admin url /admin 200 /admin/ 200 |
| 640 | // pattern /admin url /admin 200 /admin/ 200 | 648 | // pattern /admin/ url /admin 301 /admin/ 200 |
| 641 | // pattern /admin/ url /admin 301 /admin/ 200 | 649 | if requestPath[n-1] != '/' && requestPath+"/" == route.pattern { |
| 642 | if requestPath[n-1] != '/' && requestPath+"/" == route.pattern { | 650 | http.Redirect(w, r, requestPath+"/", 301) |
| 643 | http.Redirect(w, r, requestPath+"/", 301) | 651 | goto Admin |
| 644 | goto Admin | 652 | } |
| 645 | } | 653 | if requestPath[n-1] == '/' && route.pattern+"/" == requestPath { |
| 646 | if requestPath[n-1] == '/' && route.pattern+"/" == requestPath { | 654 | runMethod = p.getRunMethod(r.Method, context, route) |
| 647 | runMethod = p.getRunMethod(r.Method, context, route) | 655 | if runMethod != "" { |
| 648 | if runMethod != "" { | 656 | runrouter = route.controllerType |
| 649 | runrouter = route.controllerType | 657 | findrouter = true |
| 650 | findrouter = true | 658 | break |
| 651 | break | 659 | } |
| 652 | } | 660 | } |
| 653 | } | 661 | } |
| 654 | } | 662 | } | ... | ... |
-
Please register or sign in to post a comment