add filter after
Showing
2 changed files
with
93 additions
and
4 deletions
| ... | @@ -161,6 +161,21 @@ func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App { | ... | @@ -161,6 +161,21 @@ func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App { |
| 161 | return app | 161 | return app |
| 162 | } | 162 | } |
| 163 | 163 | ||
| 164 | func (app *App) FilterAfter(filter http.HandlerFunc) *App { | ||
| 165 | app.Handlers.FilterAfter(filter) | ||
| 166 | return app | ||
| 167 | } | ||
| 168 | |||
| 169 | func (app *App) FilterParamAfter(param string, filter http.HandlerFunc) *App { | ||
| 170 | app.Handlers.FilterParamAfter(param, filter) | ||
| 171 | return app | ||
| 172 | } | ||
| 173 | |||
| 174 | func (app *App) FilterPrefixPathAfter(path string, filter http.HandlerFunc) *App { | ||
| 175 | app.Handlers.FilterPrefixPathAfter(path, filter) | ||
| 176 | return app | ||
| 177 | } | ||
| 178 | |||
| 164 | func (app *App) SetViewsPath(path string) *App { | 179 | func (app *App) SetViewsPath(path string) *App { |
| 165 | ViewsPath = path | 180 | ViewsPath = path |
| 166 | return app | 181 | return app |
| ... | @@ -245,6 +260,21 @@ func FilterPrefixPath(path string, filter http.HandlerFunc) *App { | ... | @@ -245,6 +260,21 @@ func FilterPrefixPath(path string, filter http.HandlerFunc) *App { |
| 245 | return BeeApp | 260 | return BeeApp |
| 246 | } | 261 | } |
| 247 | 262 | ||
| 263 | func FilterAfter(filter http.HandlerFunc) *App { | ||
| 264 | BeeApp.FilterAfter(filter) | ||
| 265 | return BeeApp | ||
| 266 | } | ||
| 267 | |||
| 268 | func FilterParamAfter(param string, filter http.HandlerFunc) *App { | ||
| 269 | BeeApp.FilterParamAfter(param, filter) | ||
| 270 | return BeeApp | ||
| 271 | } | ||
| 272 | |||
| 273 | func FilterPrefixPathAfter(path string, filter http.HandlerFunc) *App { | ||
| 274 | BeeApp.FilterPrefixPathAfter(path, filter) | ||
| 275 | return BeeApp | ||
| 276 | } | ||
| 277 | |||
| 248 | func Run() { | 278 | func Run() { |
| 249 | if AppConfigPath != path.Join(AppPath, "conf", "app.conf") { | 279 | if AppConfigPath != path.Join(AppPath, "conf", "app.conf") { |
| 250 | err := ParseConfig() | 280 | err := ParseConfig() | ... | ... |
| ... | @@ -37,6 +37,7 @@ type ControllerRegistor struct { | ... | @@ -37,6 +37,7 @@ type ControllerRegistor struct { |
| 37 | fixrouters []*controllerInfo | 37 | fixrouters []*controllerInfo |
| 38 | enableFilter bool | 38 | enableFilter bool |
| 39 | filters []http.HandlerFunc | 39 | filters []http.HandlerFunc |
| 40 | enableAfter bool | ||
| 40 | afterFilters []http.HandlerFunc | 41 | afterFilters []http.HandlerFunc |
| 41 | enableUser bool | 42 | enableUser bool |
| 42 | userHandlers map[string]*userHandler | 43 | userHandlers map[string]*userHandler |
| ... | @@ -257,6 +258,35 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu | ... | @@ -257,6 +258,35 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu |
| 257 | }) | 258 | }) |
| 258 | } | 259 | } |
| 259 | 260 | ||
| 261 | // Filter adds the middleware after filter. | ||
| 262 | func (p *ControllerRegistor) FilterAfter(filter http.HandlerFunc) { | ||
| 263 | p.enableAfter = true | ||
| 264 | p.afterFilters = append(p.afterFilters, filter) | ||
| 265 | } | ||
| 266 | |||
| 267 | // FilterParam adds the middleware filter if the REST URL parameter exists. | ||
| 268 | func (p *ControllerRegistor) FilterParamAfter(param string, filter http.HandlerFunc) { | ||
| 269 | if !strings.HasPrefix(param, ":") { | ||
| 270 | param = ":" + param | ||
| 271 | } | ||
| 272 | |||
| 273 | p.FilterAfter(func(w http.ResponseWriter, r *http.Request) { | ||
| 274 | p := r.URL.Query().Get(param) | ||
| 275 | if len(p) > 0 { | ||
| 276 | filter(w, r) | ||
| 277 | } | ||
| 278 | }) | ||
| 279 | } | ||
| 280 | |||
| 281 | // FilterPrefixPath adds the middleware filter if the prefix path exists. | ||
| 282 | func (p *ControllerRegistor) FilterPrefixPathAfter(path string, filter http.HandlerFunc) { | ||
| 283 | p.FilterAfter(func(w http.ResponseWriter, r *http.Request) { | ||
| 284 | if strings.HasPrefix(r.URL.Path, path) { | ||
| 285 | filter(w, r) | ||
| 286 | } | ||
| 287 | }) | ||
| 288 | } | ||
| 289 | |||
| 260 | // AutoRoute | 290 | // AutoRoute |
| 261 | func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | 291 | func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 262 | defer func() { | 292 | defer func() { |
| ... | @@ -440,10 +470,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -440,10 +470,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 440 | 470 | ||
| 441 | if runrouter != nil { | 471 | if runrouter != nil { |
| 442 | //execute middleware filters | 472 | //execute middleware filters |
| 443 | for _, filter := range p.filters { | 473 | if p.enableFilter { |
| 444 | filter(w, r) | 474 | for _, filter := range p.filters { |
| 445 | if w.started { | 475 | filter(w, r) |
| 446 | return | 476 | if w.started { |
| 477 | return | ||
| 478 | } | ||
| 447 | } | 479 | } |
| 448 | } | 480 | } |
| 449 | 481 | ||
| ... | @@ -587,6 +619,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -587,6 +619,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 587 | } | 619 | } |
| 588 | method = vc.MethodByName("Finish") | 620 | method = vc.MethodByName("Finish") |
| 589 | method.Call(in) | 621 | method.Call(in) |
| 622 | //execute middleware filters | ||
| 623 | if p.enableAfter { | ||
| 624 | for _, filter := range p.afterFilters { | ||
| 625 | filter(w, r) | ||
| 626 | if w.started { | ||
| 627 | return | ||
| 628 | } | ||
| 629 | } | ||
| 630 | } | ||
| 590 | method = vc.MethodByName("Destructor") | 631 | method = vc.MethodByName("Destructor") |
| 591 | method.Call(in) | 632 | method.Call(in) |
| 592 | } | 633 | } |
| ... | @@ -608,6 +649,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -608,6 +649,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 608 | if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") { | 649 | if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") { |
| 609 | for mName, controllerType := range methodmap { | 650 | for mName, controllerType := range methodmap { |
| 610 | if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) { | 651 | if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) { |
| 652 | //execute middleware filters | ||
| 653 | if p.enableFilter { | ||
| 654 | for _, filter := range p.filters { | ||
| 655 | filter(w, r) | ||
| 656 | if w.started { | ||
| 657 | return | ||
| 658 | } | ||
| 659 | } | ||
| 660 | } | ||
| 611 | //parse params | 661 | //parse params |
| 612 | otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):] | 662 | otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):] |
| 613 | if len(otherurl) > 1 { | 663 | if len(otherurl) > 1 { |
| ... | @@ -651,6 +701,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -651,6 +701,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 651 | } | 701 | } |
| 652 | method = vc.MethodByName("Finish") | 702 | method = vc.MethodByName("Finish") |
| 653 | method.Call(in) | 703 | method.Call(in) |
| 704 | //execute middleware filters | ||
| 705 | if p.enableAfter { | ||
| 706 | for _, filter := range p.afterFilters { | ||
| 707 | filter(w, r) | ||
| 708 | if w.started { | ||
| 709 | return | ||
| 710 | } | ||
| 711 | } | ||
| 712 | } | ||
| 654 | method = vc.MethodByName("Destructor") | 713 | method = vc.MethodByName("Destructor") |
| 655 | method.Call(in) | 714 | method.Call(in) |
| 656 | // set find | 715 | // set find | ... | ... |
-
Please register or sign in to post a comment