Commit 47fc32ba authored by knightmare's avatar knightmare

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

pos can be const:
    BeforeRouter = iota
    AfterStatic
    BeforeExec
    AfterExec
    FinishRouter
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 {
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
......
...@@ -49,6 +49,7 @@ func DelStaticPath(url string) *App { ...@@ -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 { ...@@ -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") {
......
...@@ -16,6 +16,14 @@ import ( ...@@ -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 { ...@@ -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 { ...@@ -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) { ...@@ -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 ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment