diff --git a/beego.go b/beego.go index 60f87c21..672f0e82 100644 --- a/beego.go +++ b/beego.go @@ -140,54 +140,90 @@ func AutoPrefix(prefix string, c ControllerInterface) *App { } // register router for Get method +// usage: +// beego.Get("/", func(ctx *context.Context){ +// ctx.Output.Body("hello world") +// }) func Get(rootpath string, f FilterFunc) *App { BeeApp.Handlers.Get(rootpath, f) return BeeApp } // 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 { BeeApp.Handlers.Post(rootpath, f) return BeeApp } // 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 { BeeApp.Handlers.Delete(rootpath, f) return BeeApp } // 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 { BeeApp.Handlers.Put(rootpath, f) return BeeApp } // 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 { BeeApp.Handlers.Head(rootpath, f) return BeeApp } // 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 { BeeApp.Handlers.Options(rootpath, f) return BeeApp } // 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 { BeeApp.Handlers.Patch(rootpath, f) return BeeApp } // 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 { BeeApp.Handlers.Any(rootpath, f) return BeeApp } // 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 { BeeApp.Handlers.Handler(rootpath, h, options...) return BeeApp diff --git a/filter.go b/filter.go index 8fcd8c82..c30d00c9 100644 --- a/filter.go +++ b/filter.go @@ -6,10 +6,7 @@ package beego -import ( - "regexp" - "strings" -) +import "regexp" // FilterRouter defines filter operation before controller handler execution. // 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) { } 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 -} diff --git a/router.go b/router.go index 4291b81f..2085abb4 100644 --- a/router.go +++ b/router.go @@ -448,7 +448,7 @@ func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface) // [Deprecated] use InsertFilter. // Add FilterFunc with pattern for action. func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) error { - mr, err := buildFilter(pattern, filter) + mr, err := p.buildFilter(pattern, filter) if err != nil { return err } @@ -471,7 +471,7 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc // Add a FilterFunc with pattern rule and action constant. 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 { return err } @@ -480,6 +480,25 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter 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. // it can access any controller method. func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {