mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:10:54 +00:00
add filter after
This commit is contained in:
parent
1f3ae3d682
commit
50f3bd5835
30
beego.go
30
beego.go
@ -161,6 +161,21 @@ func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App {
|
|||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *App) FilterAfter(filter http.HandlerFunc) *App {
|
||||||
|
app.Handlers.FilterAfter(filter)
|
||||||
|
return app
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) FilterParamAfter(param string, filter http.HandlerFunc) *App {
|
||||||
|
app.Handlers.FilterParamAfter(param, filter)
|
||||||
|
return app
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) FilterPrefixPathAfter(path string, filter http.HandlerFunc) *App {
|
||||||
|
app.Handlers.FilterPrefixPathAfter(path, 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
|
||||||
@ -245,6 +260,21 @@ func FilterPrefixPath(path string, filter http.HandlerFunc) *App {
|
|||||||
return BeeApp
|
return BeeApp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FilterAfter(filter http.HandlerFunc) *App {
|
||||||
|
BeeApp.FilterAfter(filter)
|
||||||
|
return BeeApp
|
||||||
|
}
|
||||||
|
|
||||||
|
func FilterParamAfter(param string, filter http.HandlerFunc) *App {
|
||||||
|
BeeApp.FilterParamAfter(param, filter)
|
||||||
|
return BeeApp
|
||||||
|
}
|
||||||
|
|
||||||
|
func FilterPrefixPathAfter(path string, filter http.HandlerFunc) *App {
|
||||||
|
BeeApp.FilterPrefixPathAfter(path, filter)
|
||||||
|
return BeeApp
|
||||||
|
}
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
if AppConfigPath != path.Join(AppPath, "conf", "app.conf") {
|
if AppConfigPath != path.Join(AppPath, "conf", "app.conf") {
|
||||||
err := ParseConfig()
|
err := ParseConfig()
|
||||||
|
59
router.go
59
router.go
@ -37,6 +37,7 @@ type ControllerRegistor struct {
|
|||||||
fixrouters []*controllerInfo
|
fixrouters []*controllerInfo
|
||||||
enableFilter bool
|
enableFilter bool
|
||||||
filters []http.HandlerFunc
|
filters []http.HandlerFunc
|
||||||
|
enableAfter bool
|
||||||
afterFilters []http.HandlerFunc
|
afterFilters []http.HandlerFunc
|
||||||
enableUser bool
|
enableUser bool
|
||||||
userHandlers map[string]*userHandler
|
userHandlers map[string]*userHandler
|
||||||
@ -257,6 +258,35 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Filter adds the middleware after filter.
|
||||||
|
func (p *ControllerRegistor) FilterAfter(filter http.HandlerFunc) {
|
||||||
|
p.enableAfter = true
|
||||||
|
p.afterFilters = append(p.afterFilters, filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterParam adds the middleware filter if the REST URL parameter exists.
|
||||||
|
func (p *ControllerRegistor) FilterParamAfter(param string, filter http.HandlerFunc) {
|
||||||
|
if !strings.HasPrefix(param, ":") {
|
||||||
|
param = ":" + param
|
||||||
|
}
|
||||||
|
|
||||||
|
p.FilterAfter(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p := r.URL.Query().Get(param)
|
||||||
|
if len(p) > 0 {
|
||||||
|
filter(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterPrefixPath adds the middleware filter if the prefix path exists.
|
||||||
|
func (p *ControllerRegistor) FilterPrefixPathAfter(path string, filter http.HandlerFunc) {
|
||||||
|
p.FilterAfter(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if strings.HasPrefix(r.URL.Path, path) {
|
||||||
|
filter(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// AutoRoute
|
// AutoRoute
|
||||||
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -440,12 +470,14 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
if runrouter != nil {
|
if runrouter != nil {
|
||||||
//execute middleware filters
|
//execute middleware filters
|
||||||
|
if p.enableFilter {
|
||||||
for _, filter := range p.filters {
|
for _, filter := range p.filters {
|
||||||
filter(w, r)
|
filter(w, r)
|
||||||
if w.started {
|
if w.started {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Invoke the request handler
|
//Invoke the request handler
|
||||||
vc := reflect.New(runrouter.controllerType)
|
vc := reflect.New(runrouter.controllerType)
|
||||||
@ -587,6 +619,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
method = vc.MethodByName("Finish")
|
method = vc.MethodByName("Finish")
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
|
//execute middleware filters
|
||||||
|
if p.enableAfter {
|
||||||
|
for _, filter := range p.afterFilters {
|
||||||
|
filter(w, r)
|
||||||
|
if w.started {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
method = vc.MethodByName("Destructor")
|
method = vc.MethodByName("Destructor")
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
}
|
}
|
||||||
@ -608,6 +649,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") {
|
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") {
|
||||||
for mName, controllerType := range methodmap {
|
for mName, controllerType := range methodmap {
|
||||||
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) {
|
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) {
|
||||||
|
//execute middleware filters
|
||||||
|
if p.enableFilter {
|
||||||
|
for _, filter := range p.filters {
|
||||||
|
filter(w, r)
|
||||||
|
if w.started {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
//parse params
|
//parse params
|
||||||
otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):]
|
otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):]
|
||||||
if len(otherurl) > 1 {
|
if len(otherurl) > 1 {
|
||||||
@ -651,6 +701,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
method = vc.MethodByName("Finish")
|
method = vc.MethodByName("Finish")
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
|
//execute middleware filters
|
||||||
|
if p.enableAfter {
|
||||||
|
for _, filter := range p.afterFilters {
|
||||||
|
filter(w, r)
|
||||||
|
if w.started {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
method = vc.MethodByName("Destructor")
|
method = vc.MethodByName("Destructor")
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
// set find
|
// set find
|
||||||
|
Loading…
Reference in New Issue
Block a user