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

add router filter enable flag

This commit is contained in:
JessonChan 2016-03-15 18:37:54 +08:00
parent c51bc86d3f
commit 34615ee8fc

View File

@ -114,7 +114,8 @@ type controllerInfo struct {
type ControllerRegister struct { type ControllerRegister struct {
routers map[string]*Tree routers map[string]*Tree
enableFilter bool enableFilter bool
filters [5][]*FilterRouter filters [FinishRouter + 1][]*FilterRouter
filterFlag [FinishRouter + 1]bool
pool sync.Pool pool sync.Pool
} }
@ -430,6 +431,7 @@ func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) (err
return return
} }
p.enableFilter = true p.enableFilter = true
p.filterFlag[pos] = true
p.filters[pos] = append(p.filters[pos], mr) p.filters[pos] = append(p.filters[pos], mr)
return nil return nil
} }
@ -579,8 +581,8 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
return false, "" return false, ""
} }
func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath string, l []*FilterRouter) (started bool) { func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath string, pos int) (started bool) {
for _, filterR := range l { for _, filterR := range p.filters[pos] {
if filterR.returnOnOutput && context.ResponseWriter.Started { if filterR.returnOnOutput && context.ResponseWriter.Started {
return true return true
} }
@ -628,11 +630,12 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
// filter for static file // filter for static file
if fs := p.filters[BeforeStatic]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { if p.filterFlag[BeforeStatic] && p.execFilter(context, urlPath, BeforeStatic) {
goto Admin goto Admin
} }
serverStaticRouter(context) serverStaticRouter(context)
if context.ResponseWriter.Started { if context.ResponseWriter.Started {
findRouter = true findRouter = true
goto Admin goto Admin
@ -660,7 +663,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
}() }()
} }
if fs := p.filters[BeforeRouter]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { if p.filterFlag[BeforeRouter] && p.execFilter(context, urlPath, BeforeRouter) {
goto Admin goto Admin
} }
@ -689,7 +692,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if findRouter { if findRouter {
//execute middleware filters //execute middleware filters
if fs := p.filters[BeforeExec]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { if p.filterFlag[BeforeExec] && p.execFilter(context, urlPath, BeforeExec) {
goto Admin goto Admin
} }
isRunnable := false isRunnable := false
@ -790,11 +793,11 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
//execute middleware filters //execute middleware filters
if fs := p.filters[AfterExec]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { if p.filterFlag[AfterExec] && p.execFilter(context, urlPath, AfterExec) {
goto Admin goto Admin
} }
} }
if fs := p.filters[FinishRouter]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { if p.filterFlag[FinishRouter] && p.execFilter(context, urlPath, FinishRouter) {
goto Admin goto Admin
} }