1
0
mirror of https://github.com/astaxie/beego.git synced 2024-07-01 10:24:13 +00:00

Merge pull request #375 from pengfei-xue/devel

add do_filter func to reduce duplicated code
This commit is contained in:
astaxie 2013-12-15 05:25:46 -08:00
commit f841b5962b

113
router.go
View File

@ -426,6 +426,24 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
context.Output.Context = context context.Output.Context = context
context.Output.EnableGzip = EnableGzip context.Output.EnableGzip = EnableGzip
do_filter := func(pos int) (started bool) {
if p.enableFilter {
if l, ok := p.filters[pos]; ok {
for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
context.Input.Params = p
filterR.filterFunc(context)
if w.started {
return true
}
}
}
}
}
return false
}
if context.Input.IsWebsocket() { if context.Input.IsWebsocket() {
context.ResponseWriter = rw context.ResponseWriter = rw
context.Output = beecontext.NewOutput(rw) context.Output = beecontext.NewOutput(rw)
@ -436,19 +454,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
goto Admin goto Admin
} }
if p.enableFilter { if do_filter(BeforeRouter) {
if l, ok := p.filters[BeforeRouter]; ok {
for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
context.Input.Params = p
filterR.filterFunc(context)
if w.started {
goto Admin goto Admin
} }
}
}
}
}
//static file server //static file server
for prefix, staticDir := range StaticDir { for prefix, staticDir := range StaticDir {
@ -516,19 +524,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
context.Input.CruSession = GlobalSessions.SessionStart(w, r) context.Input.CruSession = GlobalSessions.SessionStart(w, r)
} }
if p.enableFilter { if do_filter(AfterStatic) {
if l, ok := p.filters[AfterStatic]; ok {
for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
context.Input.Params = p
filterR.filterFunc(context)
if w.started {
goto Admin goto Admin
} }
}
}
}
}
if CopyRequestBody { if CopyRequestBody {
context.Input.Body() context.Input.Body()
@ -592,19 +590,10 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
r.ParseMultipartForm(MaxMemory) r.ParseMultipartForm(MaxMemory)
} }
//execute middleware filters //execute middleware filters
if p.enableFilter { if do_filter(BeforeExec) {
if l, ok := p.filters[BeforeExec]; ok {
for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
context.Input.Params = p
filterR.filterFunc(context)
if w.started {
goto Admin goto Admin
} }
}
}
}
}
//Invoke the request handler //Invoke the request handler
vc := reflect.New(runrouter.controllerType) vc := reflect.New(runrouter.controllerType)
@ -747,20 +736,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
method = vc.MethodByName("Finish") method = vc.MethodByName("Finish")
method.Call(in) method.Call(in)
//execute middleware filters //execute middleware filters
if p.enableFilter { if do_filter(AfterExec) {
if l, ok := p.filters[AfterExec]; ok {
for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
context.Input.Params = p
filterR.filterFunc(context)
if w.started {
goto Admin goto Admin
} }
}
}
}
}
method = vc.MethodByName("Destructor") method = vc.MethodByName("Destructor")
method.Call(in) method.Call(in)
} }
@ -797,20 +778,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
// set find // set find
findrouter = true findrouter = true
//execute middleware filters //execute middleware filters
if p.enableFilter { if do_filter(BeforeExec) {
if l, ok := p.filters[BeforeExec]; ok {
for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
context.Input.Params = p
filterR.filterFunc(context)
if w.started {
goto Admin goto Admin
} }
}
}
}
}
//parse params //parse params
otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):] otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):]
if len(otherurl) > 1 { if len(otherurl) > 1 {
@ -853,22 +826,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
method = vc.MethodByName("Finish") method = vc.MethodByName("Finish")
method.Call(in) method.Call(in)
//execute middleware filters //execute middleware filters
if p.enableFilter { if do_filter(AfterExec) {
if l, ok := p.filters[AfterExec]; ok {
for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
context.Input.Params = p
filterR.filterFunc(context)
if w.started {
goto Admin goto Admin
} }
}
}
}
}
method = vc.MethodByName("Destructor") method = vc.MethodByName("Destructor")
method.Call(in) method.Call(in)
goto Admin goto Admin
} }
} }
@ -883,19 +849,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
Admin: Admin:
if p.enableFilter { do_filter(FinishRouter)
if l, ok := p.filters[FinishRouter]; ok {
for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
context.Input.Params = p
filterR.filterFunc(context)
if w.started {
break
}
}
}
}
}
//admin module record QPS //admin module record QPS
if EnableAdmin { if EnableAdmin {
timeend := time.Since(starttime) timeend := time.Since(starttime)