1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:51:29 +00:00

1. :all param default expr change from (.+) to (.*)

2. add hookfunc to support appstart hook
This commit is contained in:
astaxie 2013-12-30 15:06:51 +08:00
parent ecfd11adb4
commit 984b0cbf31
4 changed files with 75 additions and 12 deletions

View File

@ -13,6 +13,13 @@ import (
// beego web framework version. // beego web framework version.
const VERSION = "1.0.1" const VERSION = "1.0.1"
type hookfunc func() error //hook function to run
var hooks []hookfunc //hook function slice to store the hookfunc
func init() {
hooks = make([]hookfunc, 0)
}
// Router adds a patterned controller handler to BeeApp. // Router adds a patterned controller handler to BeeApp.
// it's an alias method of App.Router. // it's an alias method of App.Router.
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App { func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
@ -87,6 +94,12 @@ func InsertFilter(pattern string, pos int, filter FilterFunc) *App {
return BeeApp return BeeApp
} }
// The hookfunc will run in beego.Run()
// such as sessionInit, middlerware start, buildtemplate, admin start
func AddAPPStartHook(hf hookfunc) {
hooks = append(hooks, hf)
}
// Run beego application. // Run beego application.
// it's alias of App.Run. // it's alias of App.Run.
func Run() { func Run() {
@ -102,6 +115,14 @@ func Run() {
//init mime //init mime
initMime() initMime()
// do hooks function
for _, hk := range hooks {
err := hk()
if err != nil {
panic(err)
}
}
if SessionOn { if SessionOn {
GlobalSessions, _ = session.NewManager(SessionProvider, GlobalSessions, _ = session.NewManager(SessionProvider,
SessionName, SessionName,

View File

@ -28,6 +28,12 @@ func (mr *FilterRouter) ValidRouter(router string) (bool, map[string]string) {
if router == mr.pattern { if router == mr.pattern {
return true, nil return true, nil
} }
//pattern /admin router /admin/ match
//pattern /admin/ router /admin don't match, because url will 301 in router
if n := len(router); n > 1 && router[n-1] == '/' && router[:n-2] == mr.pattern {
return true, nil
}
if mr.hasregex { if mr.hasregex {
if !mr.regex.MatchString(router) { if !mr.regex.MatchString(router) {
return false, nil return false, nil
@ -46,7 +52,7 @@ func (mr *FilterRouter) ValidRouter(router string) (bool, map[string]string) {
return false, nil return false, nil
} }
func buildFilter(pattern string, filter FilterFunc) *FilterRouter { func buildFilter(pattern string, filter FilterFunc) (*FilterRouter, error) {
mr := new(FilterRouter) mr := new(FilterRouter)
mr.params = make(map[int]string) mr.params = make(map[int]string)
mr.filterFunc = filter mr.filterFunc = filter
@ -54,7 +60,7 @@ func buildFilter(pattern string, filter FilterFunc) *FilterRouter {
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 default 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 {
@ -77,7 +83,7 @@ func buildFilter(pattern string, filter FilterFunc) *FilterRouter {
j++ j++
} }
if strings.HasPrefix(part, "*") { if strings.HasPrefix(part, "*") {
expr := "(.+)" expr := "(.*)"
if part == "*.*" { if part == "*.*" {
mr.params[j] = ":path" mr.params[j] = ":path"
parts[i] = "([^.]+).([^.]+)" parts[i] = "([^.]+).([^.]+)"
@ -137,12 +143,11 @@ func buildFilter(pattern string, filter FilterFunc) *FilterRouter {
pattern = strings.Join(parts, "/") pattern = strings.Join(parts, "/")
regex, regexErr := regexp.Compile(pattern) regex, regexErr := regexp.Compile(pattern)
if regexErr != nil { if regexErr != nil {
//TODO add error handling here to avoid panic return nil, regexErr
panic(regexErr)
} }
mr.regex = regex mr.regex = regex
mr.hasregex = true mr.hasregex = true
} }
mr.pattern = pattern mr.pattern = pattern
return mr return mr, nil
} }

View File

@ -23,3 +23,32 @@ func TestFilter(t *testing.T) {
t.Errorf("user define func can't run") t.Errorf("user define func can't run")
} }
} }
var FilterAdminUser = func(ctx *context.Context) {
ctx.Output.Body([]byte("i am admin"))
}
// Filter pattern /admin/:all
// all url like /admin/ /admin/xie will all get filter
func TestPatternTwo(t *testing.T) {
r, _ := http.NewRequest("GET", "/admin/", nil)
w := httptest.NewRecorder()
handler := NewControllerRegistor()
handler.AddFilter("/admin/:all", "AfterStatic", FilterAdminUser)
handler.ServeHTTP(w, r)
if w.Body.String() != "i am admin" {
t.Errorf("filter /admin/ can't run")
}
}
func TestPatternThree(t *testing.T) {
r, _ := http.NewRequest("GET", "/admin/astaxie", nil)
w := httptest.NewRecorder()
handler := NewControllerRegistor()
handler.AddFilter("/admin/:all", "AfterStatic", FilterAdminUser)
handler.ServeHTTP(w, r)
if w.Body.String() != "i am admin" {
t.Errorf("filter /admin/astaxie can't run")
}
}

View File

@ -77,7 +77,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
params := make(map[int]string) params := make(map[int]string)
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 defult 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 {
@ -100,7 +100,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
j++ j++
} }
if strings.HasPrefix(part, "*") { if strings.HasPrefix(part, "*") {
expr := "(.+)" expr := "(.*)"
if part == "*.*" { if part == "*.*" {
params[j] = ":path" params[j] = ":path"
parts[i] = "([^.]+).([^.]+)" parts[i] = "([^.]+).([^.]+)"
@ -238,8 +238,11 @@ func (p *ControllerRegistor) AddAuto(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) { func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) error {
mr := buildFilter(pattern, filter) mr, err := buildFilter(pattern, filter)
if err != nil {
return err
}
switch action { switch action {
case "BeforeRouter": case "BeforeRouter":
p.filters[BeforeRouter] = append(p.filters[BeforeRouter], mr) p.filters[BeforeRouter] = append(p.filters[BeforeRouter], mr)
@ -253,13 +256,18 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc
p.filters[FinishRouter] = append(p.filters[FinishRouter], mr) p.filters[FinishRouter] = append(p.filters[FinishRouter], mr)
} }
p.enableFilter = true p.enableFilter = true
return nil
} }
// 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) { func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) error {
mr := buildFilter(pattern, filter) mr, err := buildFilter(pattern, filter)
if err != nil {
return err
}
p.filters[pos] = append(p.filters[pos], mr) p.filters[pos] = append(p.filters[pos], mr)
p.enableFilter = true p.enableFilter = true
return nil
} }
// UrlFor does another controller handler in this request function. // UrlFor does another controller handler in this request function.