Merge pull request #314 from shavac/master
add function InsertFilter to replace AddFilter by using int const to determine filter position
Showing
3 changed files
with
55 additions
and
16 deletions
| ... | @@ -96,6 +96,11 @@ func (app *App) Filter(pattern, action string, filter FilterFunc) *App { | ... | @@ -96,6 +96,11 @@ func (app *App) Filter(pattern, action string, filter FilterFunc) *App { |
| 96 | return app | 96 | return app |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | func (app *App) InsertFilter(pattern string, pos int, filter FilterFunc) *App { | ||
| 100 | app.Handlers.InsertFilter(pattern, pos, filter) | ||
| 101 | return app | ||
| 102 | } | ||
| 103 | |||
| 99 | func (app *App) SetViewsPath(path string) *App { | 104 | func (app *App) SetViewsPath(path string) *App { |
| 100 | ViewsPath = path | 105 | ViewsPath = path |
| 101 | return app | 106 | return app | ... | ... |
| ... | @@ -49,6 +49,7 @@ func DelStaticPath(url string) *App { | ... | @@ -49,6 +49,7 @@ func DelStaticPath(url string) *App { |
| 49 | return BeeApp | 49 | return BeeApp |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | //!!DEPRECATED!! use InsertFilter | ||
| 52 | //action has four values: | 53 | //action has four values: |
| 53 | //BeforRouter | 54 | //BeforRouter |
| 54 | //AfterStatic | 55 | //AfterStatic |
| ... | @@ -59,6 +60,12 @@ func AddFilter(pattern, action string, filter FilterFunc) *App { | ... | @@ -59,6 +60,12 @@ func AddFilter(pattern, action string, filter FilterFunc) *App { |
| 59 | return BeeApp | 60 | return BeeApp |
| 60 | } | 61 | } |
| 61 | 62 | ||
| 63 | func InsertFilter(pattern string, pos int, filter FilterFunc) *App { | ||
| 64 | BeeApp.InsertFilter(pattern, pos, filter) | ||
| 65 | return BeeApp | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 62 | func Run() { | 69 | func Run() { |
| 63 | //if AppConfigPath not In the conf/app.conf reParse config | 70 | //if AppConfigPath not In the conf/app.conf reParse config |
| 64 | if AppConfigPath != path.Join(AppPath, "conf", "app.conf") { | 71 | if AppConfigPath != path.Join(AppPath, "conf", "app.conf") { | ... | ... |
| ... | @@ -16,6 +16,14 @@ import ( | ... | @@ -16,6 +16,14 @@ import ( |
| 16 | "time" | 16 | "time" |
| 17 | ) | 17 | ) |
| 18 | 18 | ||
| 19 | const ( | ||
| 20 | BeforeRouter = iota | ||
| 21 | AfterStatic | ||
| 22 | BeforeExec | ||
| 23 | AfterExec | ||
| 24 | FinishRouter | ||
| 25 | ) | ||
| 26 | |||
| 19 | var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"} | 27 | var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"} |
| 20 | 28 | ||
| 21 | type controllerInfo struct { | 29 | type controllerInfo struct { |
| ... | @@ -31,7 +39,7 @@ type ControllerRegistor struct { | ... | @@ -31,7 +39,7 @@ type ControllerRegistor struct { |
| 31 | routers []*controllerInfo | 39 | routers []*controllerInfo |
| 32 | fixrouters []*controllerInfo | 40 | fixrouters []*controllerInfo |
| 33 | enableFilter bool | 41 | enableFilter bool |
| 34 | filters map[string][]*FilterRouter | 42 | filters map[int][]*FilterRouter |
| 35 | enableAuto bool | 43 | enableAuto bool |
| 36 | autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type | 44 | autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type |
| 37 | } | 45 | } |
| ... | @@ -40,7 +48,7 @@ func NewControllerRegistor() *ControllerRegistor { | ... | @@ -40,7 +48,7 @@ func NewControllerRegistor() *ControllerRegistor { |
| 40 | return &ControllerRegistor{ | 48 | return &ControllerRegistor{ |
| 41 | routers: make([]*controllerInfo, 0), | 49 | routers: make([]*controllerInfo, 0), |
| 42 | autoRouter: make(map[string]map[string]reflect.Type), | 50 | autoRouter: make(map[string]map[string]reflect.Type), |
| 43 | filters: make(map[string][]*FilterRouter), | 51 | filters: make(map[int][]*FilterRouter), |
| 44 | } | 52 | } |
| 45 | } | 53 | } |
| 46 | 54 | ||
| ... | @@ -214,18 +222,15 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) { | ... | @@ -214,18 +222,15 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) { |
| 214 | } | 222 | } |
| 215 | } | 223 | } |
| 216 | 224 | ||
| 217 | // Filter adds the middleware filter. | 225 | func buildFilter(pattern string, filter FilterFunc) *FilterRouter { |
| 218 | func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) { | ||
| 219 | p.enableFilter = true | ||
| 220 | mr := new(FilterRouter) | 226 | mr := new(FilterRouter) |
| 221 | mr.filterFunc = filter | 227 | mr.filterFunc = filter |
| 222 | |||
| 223 | parts := strings.Split(pattern, "/") | 228 | parts := strings.Split(pattern, "/") |
| 224 | j := 0 | 229 | j := 0 |
| 225 | for i, part := range parts { | 230 | for i, part := range parts { |
| 226 | if strings.HasPrefix(part, ":") { | 231 | if strings.HasPrefix(part, ":") { |
| 227 | expr := "(.+)" | 232 | expr := "(.+)" |
| 228 | //a user may choose to override the defult expression | 233 | //a user may choose to override the default expression |
| 229 | // similar to expressjs: ‘/user/:id([0-9]+)’ | 234 | // similar to expressjs: ‘/user/:id([0-9]+)’ |
| 230 | if index := strings.Index(part, "("); index != -1 { | 235 | if index := strings.Index(part, "("); index != -1 { |
| 231 | expr = part[index:] | 236 | expr = part[index:] |
| ... | @@ -252,13 +257,35 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc | ... | @@ -252,13 +257,35 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc |
| 252 | if regexErr != nil { | 257 | if regexErr != nil { |
| 253 | //TODO add error handling here to avoid panic | 258 | //TODO add error handling here to avoid panic |
| 254 | panic(regexErr) | 259 | panic(regexErr) |
| 255 | return | ||
| 256 | } | 260 | } |
| 257 | mr.regex = regex | 261 | mr.regex = regex |
| 258 | mr.hasregex = true | 262 | mr.hasregex = true |
| 259 | } | 263 | } |
| 260 | mr.pattern = pattern | 264 | mr.pattern = pattern |
| 261 | p.filters[action] = append(p.filters[action], mr) | 265 | return mr |
| 266 | } | ||
| 267 | |||
| 268 | func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) { | ||
| 269 | mr := buildFilter(pattern, filter) | ||
| 270 | switch action { | ||
| 271 | case "BeforRouter": | ||
| 272 | p.filters[BeforeRouter] = append(p.filters[BeforeRouter], mr) | ||
| 273 | case "AfterStatic": | ||
| 274 | p.filters[AfterStatic] = append(p.filters[AfterStatic], mr) | ||
| 275 | case "BeforeExec": | ||
| 276 | p.filters[BeforeExec] = append(p.filters[BeforeExec], mr) | ||
| 277 | case "AfterExec": | ||
| 278 | p.filters[AfterExec] = append(p.filters[AfterExec], mr) | ||
| 279 | case "FinishRouter": | ||
| 280 | p.filters[FinishRouter] = append(p.filters[FinishRouter], mr) | ||
| 281 | } | ||
| 282 | p.enableFilter = true | ||
| 283 | } | ||
| 284 | |||
| 285 | func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) { | ||
| 286 | mr := buildFilter(pattern, filter) | ||
| 287 | p.filters[pos] = append(p.filters[pos], mr) | ||
| 288 | p.enableFilter = true | ||
| 262 | } | 289 | } |
| 263 | 290 | ||
| 264 | func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { | 291 | func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { |
| ... | @@ -436,7 +463,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -436,7 +463,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 436 | } | 463 | } |
| 437 | 464 | ||
| 438 | if p.enableFilter { | 465 | if p.enableFilter { |
| 439 | if l, ok := p.filters["BeforRouter"]; ok { | 466 | if l, ok := p.filters[BeforeRouter]; ok { |
| 440 | for _, filterR := range l { | 467 | for _, filterR := range l { |
| 441 | if filterR.ValidRouter(r.URL.Path) { | 468 | if filterR.ValidRouter(r.URL.Path) { |
| 442 | filterR.filterFunc(context) | 469 | filterR.filterFunc(context) |
| ... | @@ -483,7 +510,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -483,7 +510,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 483 | } | 510 | } |
| 484 | 511 | ||
| 485 | if p.enableFilter { | 512 | if p.enableFilter { |
| 486 | if l, ok := p.filters["AfterStatic"]; ok { | 513 | if l, ok := p.filters[AfterStatic]; ok { |
| 487 | for _, filterR := range l { | 514 | for _, filterR := range l { |
| 488 | if filterR.ValidRouter(r.URL.Path) { | 515 | if filterR.ValidRouter(r.URL.Path) { |
| 489 | filterR.filterFunc(context) | 516 | filterR.filterFunc(context) |
| ... | @@ -558,7 +585,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -558,7 +585,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 558 | } | 585 | } |
| 559 | //execute middleware filters | 586 | //execute middleware filters |
| 560 | if p.enableFilter { | 587 | if p.enableFilter { |
| 561 | if l, ok := p.filters["BeforExec"]; ok { | 588 | if l, ok := p.filters[BeforeExec]; ok { |
| 562 | for _, filterR := range l { | 589 | for _, filterR := range l { |
| 563 | if filterR.ValidRouter(r.URL.Path) { | 590 | if filterR.ValidRouter(r.URL.Path) { |
| 564 | filterR.filterFunc(context) | 591 | filterR.filterFunc(context) |
| ... | @@ -713,7 +740,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -713,7 +740,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 713 | method.Call(in) | 740 | method.Call(in) |
| 714 | //execute middleware filters | 741 | //execute middleware filters |
| 715 | if p.enableFilter { | 742 | if p.enableFilter { |
| 716 | if l, ok := p.filters["AfterExec"]; ok { | 743 | if l, ok := p.filters[AfterExec]; ok { |
| 717 | for _, filterR := range l { | 744 | for _, filterR := range l { |
| 718 | if filterR.ValidRouter(r.URL.Path) { | 745 | if filterR.ValidRouter(r.URL.Path) { |
| 719 | filterR.filterFunc(context) | 746 | filterR.filterFunc(context) |
| ... | @@ -762,7 +789,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -762,7 +789,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 762 | findrouter = true | 789 | findrouter = true |
| 763 | //execute middleware filters | 790 | //execute middleware filters |
| 764 | if p.enableFilter { | 791 | if p.enableFilter { |
| 765 | if l, ok := p.filters["BeforExec"]; ok { | 792 | if l, ok := p.filters[BeforeExec]; ok { |
| 766 | for _, filterR := range l { | 793 | for _, filterR := range l { |
| 767 | if filterR.ValidRouter(r.URL.Path) { | 794 | if filterR.ValidRouter(r.URL.Path) { |
| 768 | filterR.filterFunc(context) | 795 | filterR.filterFunc(context) |
| ... | @@ -817,7 +844,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -817,7 +844,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 817 | method.Call(in) | 844 | method.Call(in) |
| 818 | //execute middleware filters | 845 | //execute middleware filters |
| 819 | if p.enableFilter { | 846 | if p.enableFilter { |
| 820 | if l, ok := p.filters["AfterExec"]; ok { | 847 | if l, ok := p.filters[AfterExec]; ok { |
| 821 | for _, filterR := range l { | 848 | for _, filterR := range l { |
| 822 | if filterR.ValidRouter(r.URL.Path) { | 849 | if filterR.ValidRouter(r.URL.Path) { |
| 823 | filterR.filterFunc(context) | 850 | filterR.filterFunc(context) |
| ... | @@ -845,7 +872,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -845,7 +872,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 845 | 872 | ||
| 846 | Admin: | 873 | Admin: |
| 847 | if p.enableFilter { | 874 | if p.enableFilter { |
| 848 | if l, ok := p.filters["Finish"]; ok { | 875 | if l, ok := p.filters[FinishRouter]; ok { |
| 849 | for _, filterR := range l { | 876 | for _, filterR := range l { |
| 850 | if filterR.ValidRouter(r.URL.Path) { | 877 | if filterR.ValidRouter(r.URL.Path) { |
| 851 | filterR.filterFunc(context) | 878 | filterR.filterFunc(context) | ... | ... |
-
Please register or sign in to post a comment