47fc32ba by knightmare

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

pos can be const:
    BeforeRouter = iota
    AfterStatic
    BeforeExec
    AfterExec
    FinishRouter
1 parent a0dff914
...@@ -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
...@@ -215,17 +223,15 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) { ...@@ -215,17 +223,15 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
215 } 223 }
216 224
217 // Filter adds the middleware filter. 225 // Filter adds the middleware filter.
218 func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) { 226 func buildFilter(pattern string, filter FilterFunc) *FilterRouter {
219 p.enableFilter = true
220 mr := new(FilterRouter) 227 mr := new(FilterRouter)
221 mr.filterFunc = filter 228 mr.filterFunc = filter
222
223 parts := strings.Split(pattern, "/") 229 parts := strings.Split(pattern, "/")
224 j := 0 230 j := 0
225 for i, part := range parts { 231 for i, part := range parts {
226 if strings.HasPrefix(part, ":") { 232 if strings.HasPrefix(part, ":") {
227 expr := "(.+)" 233 expr := "(.+)"
228 //a user may choose to override the defult expression 234 //a user may choose to override the default expression
229 // similar to expressjs: ‘/user/:id([0-9]+)’ 235 // similar to expressjs: ‘/user/:id([0-9]+)’
230 if index := strings.Index(part, "("); index != -1 { 236 if index := strings.Index(part, "("); index != -1 {
231 expr = part[index:] 237 expr = part[index:]
...@@ -252,13 +258,36 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc ...@@ -252,13 +258,36 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc
252 if regexErr != nil { 258 if regexErr != nil {
253 //TODO add error handling here to avoid panic 259 //TODO add error handling here to avoid panic
254 panic(regexErr) 260 panic(regexErr)
255 return
256 } 261 }
257 mr.regex = regex 262 mr.regex = regex
258 mr.hasregex = true 263 mr.hasregex = true
259 } 264 }
260 mr.pattern = pattern 265 mr.pattern = pattern
261 p.filters[action] = append(p.filters[action], mr) 266 return mr
267 }
268
269 //p.filters[action] = append(p.filters[action], mr)
270 func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) {
271 mr := buildFilter(pattern, filter)
272 switch action {
273 case "BeforRouter":
274 p.filters[BeforeRouter] = append(p.filters[BeforeRouter], mr)
275 case "AfterStatic":
276 p.filters[AfterStatic] = append(p.filters[AfterStatic], mr)
277 case "BeforeExec":
278 p.filters[BeforeExec] = append(p.filters[BeforeExec], mr)
279 case "AfterExec":
280 p.filters[AfterExec] = append(p.filters[AfterExec], mr)
281 case "FinishRouter":
282 p.filters[FinishRouter] = append(p.filters[FinishRouter], mr)
283 }
284 p.enableFilter = true
285 }
286
287 func (p *ControllerRegistor) InsertFilter(pattern string, filterPos int, filter FilterFunc) {
288 mr := buildFilter(pattern, filter)
289 p.filters[filterPos] = append(p.filters[filterPos], mr)
290 p.enableFilter = true
262 } 291 }
263 292
264 func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { 293 func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
...@@ -436,7 +465,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -436,7 +465,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
436 } 465 }
437 466
438 if p.enableFilter { 467 if p.enableFilter {
439 if l, ok := p.filters["BeforRouter"]; ok { 468 if l, ok := p.filters[BeforeRouter]; ok {
440 for _, filterR := range l { 469 for _, filterR := range l {
441 if filterR.ValidRouter(r.URL.Path) { 470 if filterR.ValidRouter(r.URL.Path) {
442 filterR.filterFunc(context) 471 filterR.filterFunc(context)
...@@ -483,7 +512,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -483,7 +512,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
483 } 512 }
484 513
485 if p.enableFilter { 514 if p.enableFilter {
486 if l, ok := p.filters["AfterStatic"]; ok { 515 if l, ok := p.filters[AfterStatic]; ok {
487 for _, filterR := range l { 516 for _, filterR := range l {
488 if filterR.ValidRouter(r.URL.Path) { 517 if filterR.ValidRouter(r.URL.Path) {
489 filterR.filterFunc(context) 518 filterR.filterFunc(context)
...@@ -558,7 +587,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -558,7 +587,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
558 } 587 }
559 //execute middleware filters 588 //execute middleware filters
560 if p.enableFilter { 589 if p.enableFilter {
561 if l, ok := p.filters["BeforExec"]; ok { 590 if l, ok := p.filters[BeforeExec]; ok {
562 for _, filterR := range l { 591 for _, filterR := range l {
563 if filterR.ValidRouter(r.URL.Path) { 592 if filterR.ValidRouter(r.URL.Path) {
564 filterR.filterFunc(context) 593 filterR.filterFunc(context)
...@@ -713,7 +742,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -713,7 +742,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
713 method.Call(in) 742 method.Call(in)
714 //execute middleware filters 743 //execute middleware filters
715 if p.enableFilter { 744 if p.enableFilter {
716 if l, ok := p.filters["AfterExec"]; ok { 745 if l, ok := p.filters[AfterExec]; ok {
717 for _, filterR := range l { 746 for _, filterR := range l {
718 if filterR.ValidRouter(r.URL.Path) { 747 if filterR.ValidRouter(r.URL.Path) {
719 filterR.filterFunc(context) 748 filterR.filterFunc(context)
...@@ -762,7 +791,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -762,7 +791,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
762 findrouter = true 791 findrouter = true
763 //execute middleware filters 792 //execute middleware filters
764 if p.enableFilter { 793 if p.enableFilter {
765 if l, ok := p.filters["BeforExec"]; ok { 794 if l, ok := p.filters[BeforeExec]; ok {
766 for _, filterR := range l { 795 for _, filterR := range l {
767 if filterR.ValidRouter(r.URL.Path) { 796 if filterR.ValidRouter(r.URL.Path) {
768 filterR.filterFunc(context) 797 filterR.filterFunc(context)
...@@ -817,7 +846,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -817,7 +846,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
817 method.Call(in) 846 method.Call(in)
818 //execute middleware filters 847 //execute middleware filters
819 if p.enableFilter { 848 if p.enableFilter {
820 if l, ok := p.filters["AfterExec"]; ok { 849 if l, ok := p.filters[AfterExec]; ok {
821 for _, filterR := range l { 850 for _, filterR := range l {
822 if filterR.ValidRouter(r.URL.Path) { 851 if filterR.ValidRouter(r.URL.Path) {
823 filterR.filterFunc(context) 852 filterR.filterFunc(context)
...@@ -845,7 +874,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -845,7 +874,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
845 874
846 Admin: 875 Admin:
847 if p.enableFilter { 876 if p.enableFilter {
848 if l, ok := p.filters["Finish"]; ok { 877 if l, ok := p.filters[FinishRouter]; ok {
849 for _, filterR := range l { 878 for _, filterR := range l {
850 if filterR.ValidRouter(r.URL.Path) { 879 if filterR.ValidRouter(r.URL.Path) {
851 filterR.filterFunc(context) 880 filterR.filterFunc(context)
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!