1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 17:50:58 +00:00

New func to find router info for context

This commit is contained in:
ysqi 2016-06-05 14:03:20 +08:00
parent b7e3402995
commit 2c1cea08dd

View File

@ -603,6 +603,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
findRouter bool findRouter bool
runMethod string runMethod string
routerInfo *controllerInfo routerInfo *controllerInfo
isRunnable bool
) )
context := p.pool.Get().(*beecontext.Context) context := p.pool.Get().(*beecontext.Context)
context.Reset(rw, r) context.Reset(rw, r)
@ -666,35 +667,26 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
goto Admin goto Admin
} }
if !findRouter { routerInfo, findRouter = p.FindRouter(context)
httpMethod := r.Method
if t, ok := p.routers[httpMethod]; ok {
runObject := t.Match(urlPath, context)
if r, ok := runObject.(*controllerInfo); ok {
routerInfo = r
findRouter = true
if splat := context.Input.Param(":splat"); splat != "" {
for k, v := range strings.Split(splat, "/") {
context.Input.SetParam(strconv.Itoa(k), v)
}
}
}
}
}
//if no matches to url, throw a not found exception //if no matches to url, throw a not found exception
if !findRouter { if !findRouter {
exception("404", context) exception("404", context)
goto Admin goto Admin
} }
if splat := context.Input.Param(":splat"); splat != "" {
for k, v := range strings.Split(splat, "/") {
context.Input.SetParam(strconv.Itoa(k), v)
}
}
//store router pattern into context
context.Input.SetData("RouterPattern", routerInfo.pattern)
if findRouter {
//execute middleware filters //execute middleware filters
if len(p.filters[BeforeExec]) > 0 && p.execFilter(context, urlPath, BeforeExec) { if len(p.filters[BeforeExec]) > 0 && p.execFilter(context, urlPath, BeforeExec) {
goto Admin goto Admin
} }
isRunnable := false
if routerInfo != nil { if routerInfo != nil {
if routerInfo.routerType == routerTypeRESTFul { if routerInfo.routerType == routerTypeRESTFul {
if _, ok := routerInfo.methods[r.Method]; ok { if _, ok := routerInfo.methods[r.Method]; ok {
@ -795,7 +787,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if len(p.filters[AfterExec]) > 0 && p.execFilter(context, urlPath, AfterExec) { if len(p.filters[AfterExec]) > 0 && p.execFilter(context, urlPath, AfterExec) {
goto Admin goto Admin
} }
}
if len(p.filters[FinishRouter]) > 0 && p.execFilter(context, urlPath, FinishRouter) { if len(p.filters[FinishRouter]) > 0 && p.execFilter(context, urlPath, FinishRouter) {
goto Admin goto Admin
} }
@ -836,6 +828,22 @@ Admin:
} }
} }
// FindRouter Find Router info for URL
func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *controllerInfo, isFind bool) {
var urlPath = context.Input.URL()
if !BConfig.RouterCaseSensitive {
urlPath = strings.ToLower(urlPath)
}
httpMethod := context.Input.Method()
if t, ok := p.routers[httpMethod]; ok {
runObject := t.Match(urlPath, context)
if r, ok := runObject.(*controllerInfo); ok {
return r, true
}
}
return
}
func (p *ControllerRegister) recoverPanic(context *beecontext.Context) { func (p *ControllerRegister) recoverPanic(context *beecontext.Context) {
if err := recover(); err != nil { if err := recover(); err != nil {
if err == ErrAbort { if err == ErrAbort {