1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 13:50:54 +00:00

Merge pull request #314 from shavac/master

add function InsertFilter to replace AddFilter by using int const to determine filter position
This commit is contained in:
astaxie 2013-11-25 00:56:29 -08:00
commit c7a0298546
3 changed files with 55 additions and 16 deletions

5
app.go
View File

@ -96,6 +96,11 @@ func (app *App) Filter(pattern, action string, filter FilterFunc) *App {
return app
}
func (app *App) InsertFilter(pattern string, pos int, filter FilterFunc) *App {
app.Handlers.InsertFilter(pattern, pos, filter)
return app
}
func (app *App) SetViewsPath(path string) *App {
ViewsPath = path
return app

View File

@ -49,6 +49,7 @@ func DelStaticPath(url string) *App {
return BeeApp
}
//!!DEPRECATED!! use InsertFilter
//action has four values:
//BeforRouter
//AfterStatic
@ -59,6 +60,12 @@ func AddFilter(pattern, action string, filter FilterFunc) *App {
return BeeApp
}
func InsertFilter(pattern string, pos int, filter FilterFunc) *App {
BeeApp.InsertFilter(pattern, pos, filter)
return BeeApp
}
func Run() {
//if AppConfigPath not In the conf/app.conf reParse config
if AppConfigPath != path.Join(AppPath, "conf", "app.conf") {

View File

@ -16,6 +16,14 @@ import (
"time"
)
const (
BeforeRouter = iota
AfterStatic
BeforeExec
AfterExec
FinishRouter
)
var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"}
type controllerInfo struct {
@ -31,7 +39,7 @@ type ControllerRegistor struct {
routers []*controllerInfo
fixrouters []*controllerInfo
enableFilter bool
filters map[string][]*FilterRouter
filters map[int][]*FilterRouter
enableAuto bool
autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type
}
@ -40,7 +48,7 @@ func NewControllerRegistor() *ControllerRegistor {
return &ControllerRegistor{
routers: make([]*controllerInfo, 0),
autoRouter: make(map[string]map[string]reflect.Type),
filters: make(map[string][]*FilterRouter),
filters: make(map[int][]*FilterRouter),
}
}
@ -214,18 +222,15 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
}
}
// Filter adds the middleware filter.
func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) {
p.enableFilter = true
func buildFilter(pattern string, filter FilterFunc) *FilterRouter {
mr := new(FilterRouter)
mr.filterFunc = filter
parts := strings.Split(pattern, "/")
j := 0
for i, part := range parts {
if strings.HasPrefix(part, ":") {
expr := "(.+)"
//a user may choose to override the defult expression
//a user may choose to override the default expression
// similar to expressjs: /user/:id([0-9]+)
if index := strings.Index(part, "("); index != -1 {
expr = part[index:]
@ -252,13 +257,35 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc
if regexErr != nil {
//TODO add error handling here to avoid panic
panic(regexErr)
return
}
mr.regex = regex
mr.hasregex = true
}
mr.pattern = pattern
p.filters[action] = append(p.filters[action], mr)
return mr
}
func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) {
mr := buildFilter(pattern, filter)
switch action {
case "BeforRouter":
p.filters[BeforeRouter] = append(p.filters[BeforeRouter], mr)
case "AfterStatic":
p.filters[AfterStatic] = append(p.filters[AfterStatic], mr)
case "BeforeExec":
p.filters[BeforeExec] = append(p.filters[BeforeExec], mr)
case "AfterExec":
p.filters[AfterExec] = append(p.filters[AfterExec], mr)
case "FinishRouter":
p.filters[FinishRouter] = append(p.filters[FinishRouter], mr)
}
p.enableFilter = true
}
func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) {
mr := buildFilter(pattern, filter)
p.filters[pos] = append(p.filters[pos], mr)
p.enableFilter = true
}
func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
@ -436,7 +463,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
if p.enableFilter {
if l, ok := p.filters["BeforRouter"]; ok {
if l, ok := p.filters[BeforeRouter]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
@ -483,7 +510,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
if p.enableFilter {
if l, ok := p.filters["AfterStatic"]; ok {
if l, ok := p.filters[AfterStatic]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
@ -558,7 +585,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
//execute middleware filters
if p.enableFilter {
if l, ok := p.filters["BeforExec"]; ok {
if l, ok := p.filters[BeforeExec]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
@ -713,7 +740,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
method.Call(in)
//execute middleware filters
if p.enableFilter {
if l, ok := p.filters["AfterExec"]; ok {
if l, ok := p.filters[AfterExec]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
@ -762,7 +789,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
findrouter = true
//execute middleware filters
if p.enableFilter {
if l, ok := p.filters["BeforExec"]; ok {
if l, ok := p.filters[BeforeExec]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
@ -817,7 +844,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
method.Call(in)
//execute middleware filters
if p.enableFilter {
if l, ok := p.filters["AfterExec"]; ok {
if l, ok := p.filters[AfterExec]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
@ -845,7 +872,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
Admin:
if p.enableFilter {
if l, ok := p.filters["Finish"]; ok {
if l, ok := p.filters[FinishRouter]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)