From 5588bfc35e6df9d1fb9521db9c0a99804fb1221f Mon Sep 17 00:00:00 2001 From: "asta.xie" Date: Sat, 29 Mar 2014 14:55:34 +0800 Subject: [PATCH] support filter to get router. get runController & runMethod --- context/input.go | 13 ++++++++----- router.go | 50 ++++++++++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/context/input.go b/context/input.go index 308dd77c..0a3c8535 100644 --- a/context/input.go +++ b/context/input.go @@ -4,6 +4,7 @@ import ( "bytes" "io/ioutil" "net/http" + "reflect" "strconv" "strings" @@ -13,11 +14,13 @@ import ( // BeegoInput operates the http request header ,data ,cookie and body. // it also contains router params and current session. type BeegoInput struct { - CruSession session.SessionStore - Params map[string]string - Data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. - Request *http.Request - RequestBody []byte + CruSession session.SessionStore + Params map[string]string + Data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. + Request *http.Request + RequestBody []byte + RunController reflect.Type + RunMethod string } // NewInput return BeegoInput generated by http.Request. diff --git a/router.go b/router.go index 9a1ec6ff..6541c4ee 100644 --- a/router.go +++ b/router.go @@ -626,29 +626,37 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) context.Input.Body() } + if context.Input.RunController != nil && context.Input.RunMethod { + findrouter = true + runMethod = context.Input.RunMethod + runrouter = context.Input.RunController + } + //first find path from the fixrouters to Improve Performance - for _, route := range p.fixrouters { - n := len(requestPath) - if requestPath == route.pattern { - runMethod = p.getRunMethod(r.Method, context, route) - if runMethod != "" { - runrouter = route.controllerType - findrouter = true - break + if !findrouter { + for _, route := range p.fixrouters { + n := len(requestPath) + if requestPath == route.pattern { + runMethod = p.getRunMethod(r.Method, context, route) + if runMethod != "" { + runrouter = route.controllerType + findrouter = true + break + } } - } - // pattern /admin url /admin 200 /admin/ 200 - // pattern /admin/ url /admin 301 /admin/ 200 - if requestPath[n-1] != '/' && requestPath+"/" == route.pattern { - http.Redirect(w, r, requestPath+"/", 301) - goto Admin - } - if requestPath[n-1] == '/' && route.pattern+"/" == requestPath { - runMethod = p.getRunMethod(r.Method, context, route) - if runMethod != "" { - runrouter = route.controllerType - findrouter = true - break + // pattern /admin url /admin 200 /admin/ 200 + // pattern /admin/ url /admin 301 /admin/ 200 + if requestPath[n-1] != '/' && requestPath+"/" == route.pattern { + http.Redirect(w, r, requestPath+"/", 301) + goto Admin + } + if requestPath[n-1] == '/' && route.pattern+"/" == requestPath { + runMethod = p.getRunMethod(r.Method, context, route) + if runMethod != "" { + runrouter = route.controllerType + findrouter = true + break + } } } }