mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 05:01:28 +00:00
Merge pull request #1799 from JessonChan/router_develop
Router Filter Improve
This commit is contained in:
commit
9e9671d8cd
2
admin.go
2
admin.go
@ -196,7 +196,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
|
||||
BeforeExec: "Before Exec",
|
||||
AfterExec: "After Exec",
|
||||
FinishRouter: "Finish Router"} {
|
||||
if bf, ok := BeeApp.Handlers.filters[k]; ok {
|
||||
if bf := BeeApp.Handlers.filters[k]; len(bf) > 0 {
|
||||
filterType = fr
|
||||
filterTypes = append(filterTypes, filterType)
|
||||
resultList := new([][]string)
|
||||
|
@ -44,7 +44,7 @@ func NewNamespace(prefix string, params ...LinkNamespace) *Namespace {
|
||||
return ns
|
||||
}
|
||||
|
||||
// Cond set condtion function
|
||||
// Cond set condition function
|
||||
// if cond return true can run this namespace, else can't
|
||||
// usage:
|
||||
// ns.Cond(func (ctx *context.Context) bool{
|
||||
@ -60,7 +60,7 @@ func (n *Namespace) Cond(cond namespaceCond) *Namespace {
|
||||
exception("405", ctx)
|
||||
}
|
||||
}
|
||||
if v, ok := n.handlers.filters[BeforeRouter]; ok {
|
||||
if v := n.handlers.filters[BeforeRouter]; len(v) > 0 {
|
||||
mr := new(FilterRouter)
|
||||
mr.tree = NewTree()
|
||||
mr.pattern = "*"
|
||||
|
44
router.go
44
router.go
@ -114,7 +114,7 @@ type controllerInfo struct {
|
||||
type ControllerRegister struct {
|
||||
routers map[string]*Tree
|
||||
enableFilter bool
|
||||
filters map[int][]*FilterRouter
|
||||
filters [FinishRouter + 1][]*FilterRouter
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
@ -122,7 +122,6 @@ type ControllerRegister struct {
|
||||
func NewControllerRegister() *ControllerRegister {
|
||||
cr := &ControllerRegister{
|
||||
routers: make(map[string]*Tree),
|
||||
filters: make(map[int][]*FilterRouter),
|
||||
}
|
||||
cr.pool.New = func() interface{} {
|
||||
return beecontext.NewContext()
|
||||
@ -408,7 +407,6 @@ func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface)
|
||||
// InsertFilter Add a FilterFunc with pattern rule and action constant.
|
||||
// The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
|
||||
func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) error {
|
||||
|
||||
mr := new(FilterRouter)
|
||||
mr.tree = NewTree()
|
||||
mr.pattern = pattern
|
||||
@ -426,9 +424,13 @@ func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter Filter
|
||||
}
|
||||
|
||||
// add Filter into
|
||||
func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) error {
|
||||
p.filters[pos] = append(p.filters[pos], mr)
|
||||
func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) (err error) {
|
||||
if pos < BeforeStatic || pos > FinishRouter {
|
||||
err = fmt.Errorf("can not find your filter postion")
|
||||
return
|
||||
}
|
||||
p.enableFilter = true
|
||||
p.filters[pos] = append(p.filters[pos], mr)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -577,10 +579,8 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func (p *ControllerRegister) execFilter(context *beecontext.Context, pos int, urlPath string) (started bool) {
|
||||
if p.enableFilter {
|
||||
if l, ok := p.filters[pos]; ok {
|
||||
for _, filterR := range l {
|
||||
func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath string, pos int) (started bool) {
|
||||
for _, filterR := range p.filters[pos] {
|
||||
if filterR.returnOnOutput && context.ResponseWriter.Started {
|
||||
return true
|
||||
}
|
||||
@ -591,8 +591,6 @@ func (p *ControllerRegister) execFilter(context *beecontext.Context, pos int, ur
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@ -617,11 +615,10 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
context.Output.Header("Server", BConfig.ServerName)
|
||||
}
|
||||
|
||||
var urlPath string
|
||||
var urlPath = r.URL.Path
|
||||
|
||||
if !BConfig.RouterCaseSensitive {
|
||||
urlPath = strings.ToLower(r.URL.Path)
|
||||
} else {
|
||||
urlPath = r.URL.Path
|
||||
urlPath = strings.ToLower(urlPath)
|
||||
}
|
||||
|
||||
// filter wrong http method
|
||||
@ -631,11 +628,12 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
// filter for static file
|
||||
if p.execFilter(context, BeforeStatic, urlPath) {
|
||||
if len(p.filters[BeforeStatic]) > 0 && p.execFilter(context, urlPath, BeforeStatic) {
|
||||
goto Admin
|
||||
}
|
||||
|
||||
serverStaticRouter(context)
|
||||
|
||||
if context.ResponseWriter.Started {
|
||||
findRouter = true
|
||||
goto Admin
|
||||
@ -655,7 +653,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
if err != nil {
|
||||
Error(err)
|
||||
exception("503", context)
|
||||
return
|
||||
goto Admin
|
||||
}
|
||||
defer func() {
|
||||
if context.Input.CruSession != nil {
|
||||
@ -663,8 +661,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if p.execFilter(context, BeforeRouter, urlPath) {
|
||||
if len(p.filters[BeforeRouter]) > 0 && p.execFilter(context, urlPath, BeforeRouter) {
|
||||
goto Admin
|
||||
}
|
||||
|
||||
@ -693,7 +690,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
|
||||
if findRouter {
|
||||
//execute middleware filters
|
||||
if p.execFilter(context, BeforeExec, urlPath) {
|
||||
if len(p.filters[BeforeExec]) > 0 && p.execFilter(context, urlPath, BeforeExec) {
|
||||
goto Admin
|
||||
}
|
||||
isRunnable := false
|
||||
@ -794,12 +791,13 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
//execute middleware filters
|
||||
if p.execFilter(context, AfterExec, urlPath) {
|
||||
if len(p.filters[AfterExec]) > 0 && p.execFilter(context, urlPath, AfterExec) {
|
||||
goto Admin
|
||||
}
|
||||
}
|
||||
|
||||
p.execFilter(context, FinishRouter, urlPath)
|
||||
if len(p.filters[FinishRouter]) > 0 && p.execFilter(context, urlPath, FinishRouter) {
|
||||
goto Admin
|
||||
}
|
||||
|
||||
Admin:
|
||||
//admin module record QPS
|
||||
|
Loading…
Reference in New Issue
Block a user