From 868e14b8ba1b1a8664596021e5329929bb735856 Mon Sep 17 00:00:00 2001 From: astaxie Date: Thu, 15 Sep 2016 20:04:45 +0800 Subject: [PATCH] fix #2017 --- context/input.go | 14 ++++++++------ router.go | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/context/input.go b/context/input.go index 5af8e6c4..1e6eaf71 100644 --- a/context/input.go +++ b/context/input.go @@ -40,12 +40,14 @@ var ( // BeegoInput operates the http request header, data, cookie and body. // it also contains router params and current session. type BeegoInput struct { - Context *Context - CruSession session.Store - pnames []string - pvalues []string - data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. - RequestBody []byte + Context *Context + CruSession session.Store + pnames []string + pvalues []string + data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. + RequestBody []byte + RunMethod string + RunController reflect.Type } // NewInput return BeegoInput generated by Context. diff --git a/router.go b/router.go index 835b10b2..97b0edba 100644 --- a/router.go +++ b/router.go @@ -685,8 +685,16 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) if len(p.filters[BeforeRouter]) > 0 && p.execFilter(context, urlPath, BeforeRouter) { goto Admin } + // User can define RunController and RunMethod in filter + if context.Input.RunController != nil && context.Input.RunMethod != "" { + findRouter = true + isRunnable = true + runMethod = context.Input.RunMethod + runRouter = context.Input.RunController + } else { + routerInfo, findRouter = p.FindRouter(context) + } - routerInfo, findRouter = p.FindRouter(context) //if no matches to url, throw a not found exception if !findRouter { exception("404", context) @@ -698,15 +706,16 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } } - //store router pattern into context - context.Input.SetData("RouterPattern", routerInfo.pattern) - //execute middleware filters if len(p.filters[BeforeExec]) > 0 && p.execFilter(context, urlPath, BeforeExec) { goto Admin } if routerInfo != nil { + if BConfig.RunMode == DEV { + //store router pattern into context + context.Input.SetData("RouterPattern", routerInfo.pattern) + } if routerInfo.routerType == routerTypeRESTFul { if _, ok := routerInfo.methods[r.Method]; ok { isRunnable = true