diff --git a/app.go b/app.go index 43b5e673..a33b6f77 100644 --- a/app.go +++ b/app.go @@ -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 diff --git a/beego.go b/beego.go index aa0dc85b..32fdffd0 100644 --- a/beego.go +++ b/beego.go @@ -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") { diff --git a/router.go b/router.go index 1cf5b5cc..05465ef2 100644 --- a/router.go +++ b/router.go @@ -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)