Commit 17104c25 authored by astaxie's avatar astaxie

beego: Refactoring Filter & add comments

parent 8b374d7f
...@@ -140,54 +140,90 @@ func AutoPrefix(prefix string, c ControllerInterface) *App { ...@@ -140,54 +140,90 @@ func AutoPrefix(prefix string, c ControllerInterface) *App {
} }
// register router for Get method // register router for Get method
// usage:
// beego.Get("/", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Get(rootpath string, f FilterFunc) *App { func Get(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Get(rootpath, f) BeeApp.Handlers.Get(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Post method // register router for Post method
// usage:
// beego.Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Post(rootpath string, f FilterFunc) *App { func Post(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Post(rootpath, f) BeeApp.Handlers.Post(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Delete method // register router for Delete method
// usage:
// beego.Delete("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Delete(rootpath string, f FilterFunc) *App { func Delete(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Delete(rootpath, f) BeeApp.Handlers.Delete(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Put method // register router for Put method
// usage:
// beego.Put("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Put(rootpath string, f FilterFunc) *App { func Put(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Put(rootpath, f) BeeApp.Handlers.Put(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Head method // register router for Head method
// usage:
// beego.Head("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Head(rootpath string, f FilterFunc) *App { func Head(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Head(rootpath, f) BeeApp.Handlers.Head(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Options method // register router for Options method
// usage:
// beego.Options("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Options(rootpath string, f FilterFunc) *App { func Options(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Options(rootpath, f) BeeApp.Handlers.Options(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Patch method // register router for Patch method
// usage:
// beego.Patch("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Patch(rootpath string, f FilterFunc) *App { func Patch(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Patch(rootpath, f) BeeApp.Handlers.Patch(rootpath, f)
return BeeApp return BeeApp
} }
// register router for all method // register router for all method
// usage:
// beego.Any("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Any(rootpath string, f FilterFunc) *App { func Any(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Any(rootpath, f) BeeApp.Handlers.Any(rootpath, f)
return BeeApp return BeeApp
} }
// register router for own Handler // register router for own Handler
// usage:
// beego.Handler("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Handler(rootpath string, h http.Handler, options ...interface{}) *App { func Handler(rootpath string, h http.Handler, options ...interface{}) *App {
BeeApp.Handlers.Handler(rootpath, h, options...) BeeApp.Handlers.Handler(rootpath, h, options...)
return BeeApp return BeeApp
......
...@@ -6,10 +6,7 @@ ...@@ -6,10 +6,7 @@
package beego package beego
import ( import "regexp"
"regexp"
"strings"
)
// FilterRouter defines filter operation before controller handler execution. // FilterRouter defines filter operation before controller handler execution.
// it can match patterned url and do filter function when action arrives. // it can match patterned url and do filter function when action arrives.
...@@ -57,103 +54,3 @@ func (mr *FilterRouter) ValidRouter(router string) (bool, map[string]string) { ...@@ -57,103 +54,3 @@ func (mr *FilterRouter) ValidRouter(router string) (bool, map[string]string) {
} }
return false, nil return false, nil
} }
func buildFilter(pattern string, filter FilterFunc) (*FilterRouter, error) {
mr := new(FilterRouter)
mr.params = make(map[int]string)
mr.filterFunc = filter
parts := strings.Split(pattern, "/")
j := 0
for i, part := range parts {
if strings.HasPrefix(part, ":") {
expr := "(.*)"
//a user may choose to override the default expression
// similar to expressjs: ‘/user/:id([0-9]+)’
if index := strings.Index(part, "("); index != -1 {
expr = part[index:]
part = part[:index]
//match /user/:id:int ([0-9]+)
//match /post/:username:string ([\w]+)
} else if lindex := strings.LastIndex(part, ":"); lindex != 0 {
switch part[lindex:] {
case ":int":
expr = "([0-9]+)"
part = part[:lindex]
case ":string":
expr = `([\w]+)`
part = part[:lindex]
}
}
mr.params[j] = part
parts[i] = expr
j++
}
if strings.HasPrefix(part, "*") {
expr := "(.*)"
if part == "*.*" {
mr.params[j] = ":path"
parts[i] = "([^.]+).([^.]+)"
j++
mr.params[j] = ":ext"
j++
} else {
mr.params[j] = ":splat"
parts[i] = expr
j++
}
}
//url like someprefix:id(xxx).html
if strings.Contains(part, ":") && strings.Contains(part, "(") && strings.Contains(part, ")") {
var out []rune
var start bool
var startexp bool
var param []rune
var expt []rune
for _, v := range part {
if start {
if v != '(' {
param = append(param, v)
continue
}
}
if startexp {
if v != ')' {
expt = append(expt, v)
continue
}
}
if v == ':' {
param = make([]rune, 0)
param = append(param, ':')
start = true
} else if v == '(' {
startexp = true
start = false
mr.params[j] = string(param)
j++
expt = make([]rune, 0)
expt = append(expt, '(')
} else if v == ')' {
startexp = false
expt = append(expt, ')')
out = append(out, expt...)
} else {
out = append(out, v)
}
}
parts[i] = string(out)
}
}
if j != 0 {
pattern = strings.Join(parts, "/")
regex, regexErr := regexp.Compile(pattern)
if regexErr != nil {
return nil, regexErr
}
mr.regex = regex
mr.hasregex = true
}
mr.pattern = pattern
return mr, nil
}
...@@ -448,7 +448,7 @@ func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface) ...@@ -448,7 +448,7 @@ func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface)
// [Deprecated] use InsertFilter. // [Deprecated] use InsertFilter.
// Add FilterFunc with pattern for action. // Add FilterFunc with pattern for action.
func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) error { func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) error {
mr, err := buildFilter(pattern, filter) mr, err := p.buildFilter(pattern, filter)
if err != nil { if err != nil {
return err return err
} }
...@@ -471,7 +471,7 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc ...@@ -471,7 +471,7 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc
// Add a FilterFunc with pattern rule and action constant. // Add a FilterFunc with pattern rule and action constant.
func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) error { func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) error {
mr, err := buildFilter(pattern, filter) mr, err := p.buildFilter(pattern, filter)
if err != nil { if err != nil {
return err return err
} }
...@@ -480,6 +480,25 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter ...@@ -480,6 +480,25 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter
return nil return nil
} }
func (p *ControllerRegistor) buildFilter(pattern string, filter FilterFunc) (*FilterRouter, error) {
mr := new(FilterRouter)
mr.params = make(map[int]string)
mr.filterFunc = filter
j, params, parts := p.splitRoute(pattern)
if j != 0 {
pattern = strings.Join(parts, "/")
regex, regexErr := regexp.Compile(pattern)
if regexErr != nil {
return nil, regexErr
}
mr.regex = regex
mr.hasregex = true
}
mr.params = params
mr.pattern = pattern
return mr, nil
}
// UrlFor does another controller handler in this request function. // UrlFor does another controller handler in this request function.
// it can access any controller method. // it can access any controller method.
func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
......
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