1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-19 05:10:54 +00:00

add func InsertFilter(pattern string, pos int, filter FilterFunc) *App to replace AddFilter

pos can be const:
    BeforeRouter = iota
    AfterStatic
    BeforeExec
    AfterExec
    FinishRouter
This commit is contained in:
knightmare 2013-11-25 15:59:40 +08:00
parent a0dff9148a
commit 47fc32ba47
3 changed files with 56 additions and 15 deletions

5
app.go
View File

@ -96,6 +96,11 @@ func (app *App) Filter(pattern, action string, filter FilterFunc) *App {
return 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 { func (app *App) SetViewsPath(path string) *App {
ViewsPath = path ViewsPath = path
return app return app

View File

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

View File

@ -16,6 +16,14 @@ import (
"time" "time"
) )
const (
BeforeRouter = iota
AfterStatic
BeforeExec
AfterExec
FinishRouter
)
var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"} var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"}
type controllerInfo struct { type controllerInfo struct {
@ -31,7 +39,7 @@ type ControllerRegistor struct {
routers []*controllerInfo routers []*controllerInfo
fixrouters []*controllerInfo fixrouters []*controllerInfo
enableFilter bool enableFilter bool
filters map[string][]*FilterRouter filters map[int][]*FilterRouter
enableAuto bool enableAuto bool
autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type
} }
@ -40,7 +48,7 @@ func NewControllerRegistor() *ControllerRegistor {
return &ControllerRegistor{ return &ControllerRegistor{
routers: make([]*controllerInfo, 0), routers: make([]*controllerInfo, 0),
autoRouter: make(map[string]map[string]reflect.Type), autoRouter: make(map[string]map[string]reflect.Type),
filters: make(map[string][]*FilterRouter), filters: make(map[int][]*FilterRouter),
} }
} }
@ -215,17 +223,15 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
} }
// Filter adds the middleware filter. // Filter adds the middleware filter.
func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) { func buildFilter(pattern string, filter FilterFunc) *FilterRouter {
p.enableFilter = true
mr := new(FilterRouter) mr := new(FilterRouter)
mr.filterFunc = filter mr.filterFunc = filter
parts := strings.Split(pattern, "/") parts := strings.Split(pattern, "/")
j := 0 j := 0
for i, part := range parts { for i, part := range parts {
if strings.HasPrefix(part, ":") { if strings.HasPrefix(part, ":") {
expr := "(.+)" 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]+) // similar to expressjs: /user/:id([0-9]+)
if index := strings.Index(part, "("); index != -1 { if index := strings.Index(part, "("); index != -1 {
expr = part[index:] expr = part[index:]
@ -252,13 +258,36 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc
if regexErr != nil { if regexErr != nil {
//TODO add error handling here to avoid panic //TODO add error handling here to avoid panic
panic(regexErr) panic(regexErr)
return
} }
mr.regex = regex mr.regex = regex
mr.hasregex = true mr.hasregex = true
} }
mr.pattern = pattern mr.pattern = pattern
p.filters[action] = append(p.filters[action], mr) return mr
}
//p.filters[action] = append(p.filters[action], 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, filterPos int, filter FilterFunc) {
mr := buildFilter(pattern, filter)
p.filters[filterPos] = append(p.filters[filterPos], mr)
p.enableFilter = true
} }
func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
@ -436,7 +465,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters["BeforRouter"]; ok { if l, ok := p.filters[BeforeRouter]; ok {
for _, filterR := range l { for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) { if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context) filterR.filterFunc(context)
@ -483,7 +512,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters["AfterStatic"]; ok { if l, ok := p.filters[AfterStatic]; ok {
for _, filterR := range l { for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) { if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context) filterR.filterFunc(context)
@ -558,7 +587,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
//execute middleware filters //execute middleware filters
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters["BeforExec"]; ok { if l, ok := p.filters[BeforeExec]; ok {
for _, filterR := range l { for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) { if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context) filterR.filterFunc(context)
@ -713,7 +742,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
method.Call(in) method.Call(in)
//execute middleware filters //execute middleware filters
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters["AfterExec"]; ok { if l, ok := p.filters[AfterExec]; ok {
for _, filterR := range l { for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) { if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context) filterR.filterFunc(context)
@ -762,7 +791,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
findrouter = true findrouter = true
//execute middleware filters //execute middleware filters
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters["BeforExec"]; ok { if l, ok := p.filters[BeforeExec]; ok {
for _, filterR := range l { for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) { if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context) filterR.filterFunc(context)
@ -817,7 +846,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
method.Call(in) method.Call(in)
//execute middleware filters //execute middleware filters
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters["AfterExec"]; ok { if l, ok := p.filters[AfterExec]; ok {
for _, filterR := range l { for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) { if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context) filterR.filterFunc(context)
@ -845,7 +874,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
Admin: Admin:
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters["Finish"]; ok { if l, ok := p.filters[FinishRouter]; ok {
for _, filterR := range l { for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) { if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context) filterR.filterFunc(context)