From b9fdbdf7b5a11779686bb9f59661d1526cd3231c Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 16 Dec 2013 23:22:15 +0800 Subject: [PATCH 1/4] use StopRun to terminate the execution --- controller.go | 2 +- router.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/controller.go b/controller.go index d5be2ef0..b3b3577f 100644 --- a/controller.go +++ b/controller.go @@ -181,7 +181,7 @@ func (c *Controller) Abort(code string) { } func (c *Controller) StopRun() { - panic("") + panic("StopRun") } func (c *Controller) UrlFor(endpoint string, values ...string) string { diff --git a/router.go b/router.go index 94f4279f..2bc14a87 100644 --- a/router.go +++ b/router.go @@ -369,6 +369,9 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { defer func() { if err := recover(); err != nil { + if fmt.Sprint(err) == "StopRun" { + return + } if _, ok := err.(middleware.HTTPException); ok { // catch intented errors, only for HTTP 4XX and 5XX } else { From 65b9011bc16cd7fd328aff733dd15c3494dda464 Mon Sep 17 00:00:00 2001 From: Pengfei Xue Date: Tue, 17 Dec 2013 08:18:01 +0800 Subject: [PATCH 2/4] delete write body from context.Redirect --- context/context.go | 1 - 1 file changed, 1 deletion(-) diff --git a/context/context.go b/context/context.go index 18d098f1..e2a82652 100644 --- a/context/context.go +++ b/context/context.go @@ -16,7 +16,6 @@ type Context struct { func (ctx *Context) Redirect(status int, localurl string) { ctx.Output.Header("Location", localurl) ctx.Output.SetStatus(status) - ctx.Output.Body([]byte(" ")) } func (ctx *Context) Abort(status int, body string) { From 7b27b7fed0eabd499a1870d1a70f7bdde6fab090 Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 17 Dec 2013 08:53:15 +0800 Subject: [PATCH 3/4] change SopRun to a variable --- controller.go | 6 +++++- router.go | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/controller.go b/controller.go index b3b3577f..13f358be 100644 --- a/controller.go +++ b/controller.go @@ -24,6 +24,10 @@ import ( "github.com/astaxie/beego/session" ) +var ( + USERSTOPRUN = errors.New("User stop run") +) + type Controller struct { Ctx *context.Context Data map[interface{}]interface{} @@ -181,7 +185,7 @@ func (c *Controller) Abort(code string) { } func (c *Controller) StopRun() { - panic("StopRun") + panic(USERSTOPRUN) } func (c *Controller) UrlFor(endpoint string, values ...string) string { diff --git a/router.go b/router.go index 2bc14a87..df7f5a19 100644 --- a/router.go +++ b/router.go @@ -369,7 +369,7 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { defer func() { if err := recover(); err != nil { - if fmt.Sprint(err) == "StopRun" { + if err == USERSTOPRUN { return } if _, ok := err.(middleware.HTTPException); ok { From c3bc2bedc0511e63b89ae43a02d191032377ae9f Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 17 Dec 2013 11:19:18 +0800 Subject: [PATCH 4/4] add methodName to fix #380 & arrangement the router --- controller.go | 34 +++--- router.go | 294 ++++++++++++++------------------------------------ 2 files changed, 101 insertions(+), 227 deletions(-) diff --git a/controller.go b/controller.go index 13f358be..563b0b9f 100644 --- a/controller.go +++ b/controller.go @@ -29,21 +29,22 @@ var ( ) type Controller struct { - Ctx *context.Context - Data map[interface{}]interface{} - ChildName string - TplNames string - Layout string - TplExt string - _xsrf_token string - gotofunc string - CruSession session.SessionStore - XSRFExpire int - AppController interface{} + Ctx *context.Context + Data map[interface{}]interface{} + controllerName string + actionName string + TplNames string + Layout string + TplExt string + _xsrf_token string + gotofunc string + CruSession session.SessionStore + XSRFExpire int + AppController interface{} } type ControllerInterface interface { - Init(ct *context.Context, childName string, app interface{}) + Init(ct *context.Context, controllerName, actionName string, app interface{}) Prepare() Get() Post() @@ -56,11 +57,12 @@ type ControllerInterface interface { Render() error } -func (c *Controller) Init(ctx *context.Context, childName string, app interface{}) { +func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) { c.Data = make(map[interface{}]interface{}) c.Layout = "" c.TplNames = "" - c.ChildName = childName + c.controllerName = controllerName + c.actionName = actionName c.Ctx = ctx c.TplExt = "tpl" c.AppController = app @@ -123,7 +125,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { //if the controller has set layout, then first get the tplname's content set the content to the layout if c.Layout != "" { if c.TplNames == "" { - c.TplNames = c.ChildName + "/" + strings.ToLower(c.Ctx.Request.Method) + "." + c.TplExt + c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt } if RunMode == "dev" { BuildTemplate(ViewsPath) @@ -150,7 +152,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { return icontent, nil } else { if c.TplNames == "" { - c.TplNames = c.ChildName + "/" + strings.ToLower(c.Ctx.Request.Method) + "." + c.TplExt + c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt } if RunMode == "dev" { BuildTemplate(ViewsPath) diff --git a/router.go b/router.go index df7f5a19..b73d550c 100644 --- a/router.go +++ b/router.go @@ -211,6 +211,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM } } +// add auto router to controller func (p *ControllerRegistor) AddAuto(c ControllerInterface) { p.enableAuto = true reflectVal := reflect.ValueOf(c) @@ -424,8 +425,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) starttime := time.Now() requestPath := r.URL.Path - var runrouter *controllerInfo + var runrouter reflect.Type var findrouter bool + var runMethod string params := make(map[string]string) w := &responseWriter{writer: rw} @@ -550,8 +552,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) for _, route := range p.fixrouters { n := len(requestPath) if requestPath == route.pattern { - runrouter = route + runrouter = route.controllerType findrouter = true + runMethod = p.getRunMethod(r.Method, route) break } // pattern /admin url /admin 200 /admin/ 404 @@ -591,15 +594,64 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) //reassemble query params and add to RawQuery r.URL.RawQuery = url.Values(values).Encode() } - runrouter = route + runrouter = route.controllerType findrouter = true + context.Input.Params = params + runMethod = p.getRunMethod(r.Method, route) break } } - context.Input.Params = params + if !findrouter && p.enableAuto { + // deal with url with diffirent ext + // /controller/simple + // /controller/simple.html + // /controller/simple.json + // /controller/simple.rss + lastindex := strings.LastIndex(requestPath, "/") + lastsub := requestPath[lastindex+1:] + if subindex := strings.LastIndex(lastsub, "."); subindex != -1 { + context.Input.Params[":ext"] = lastsub[subindex+1:] + r.URL.Query().Add(":ext", lastsub[subindex+1:]) + r.URL.RawQuery = r.URL.Query().Encode() + requestPath = requestPath[:len(requestPath)-len(lastsub[subindex:])] + } + for cName, methodmap := range p.autoRouter { + // if prev already find the router break + if findrouter { + break + } + if strings.ToLower(requestPath) == "/"+cName { + http.Redirect(w, r, requestPath+"/", 301) + goto Admin + } + // if there's no action, set the default action to index + if strings.ToLower(requestPath) == "/"+cName+"/" { + requestPath = requestPath + "index" + } + // if the request path start with controllerName + if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") { + for mName, controllerType := range methodmap { + if strings.ToLower(requestPath) == "/"+cName+"/"+strings.ToLower(mName) || + (strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) && + requestPath[len("/"+cName+"/"+strings.ToLower(mName)):len("/"+cName+"/"+strings.ToLower(mName))+1] == "/") { + runrouter = controllerType + runMethod = mName + findrouter = true + break + } + } + } + } + } - if runrouter != nil { + //if no matches to url, throw a not found exception + if !findrouter { + middleware.Exception("404", rw, r, "") + goto Admin + } + + if findrouter { if r.Method == "POST" { r.ParseMultipartForm(MaxMemory) } @@ -609,14 +661,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } //Invoke the request handler - vc := reflect.New(runrouter.controllerType) + vc := reflect.New(runrouter) //call the controller init function method := vc.MethodByName("Init") - in := make([]reflect.Value, 3) + in := make([]reflect.Value, 4) in[0] = reflect.ValueOf(context) - in[1] = reflect.ValueOf(runrouter.controllerType.Name()) - in[2] = reflect.ValueOf(vc.Interface()) + in[1] = reflect.ValueOf(runrouter.Name()) + in[2] = reflect.ValueOf(runMethod) + in[3] = reflect.ValueOf(vc.Interface()) method.Call(in) //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf @@ -636,110 +689,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) method = vc.MethodByName("Prepare") method.Call(in) - //if response has written,yes don't run next if !w.started { - if r.Method == "GET" { - if runrouter.hasMethod { - if m, ok := runrouter.methods["get"]; ok { - method = vc.MethodByName(m) - } else if m, ok = runrouter.methods["*"]; ok { - method = vc.MethodByName(m) - } else { - method = vc.MethodByName("Get") - } - } else { - method = vc.MethodByName("Get") - } - method.Call(in) - } else if r.Method == "HEAD" { - if runrouter.hasMethod { - if m, ok := runrouter.methods["head"]; ok { - method = vc.MethodByName(m) - } else if m, ok = runrouter.methods["*"]; ok { - method = vc.MethodByName(m) - } else { - method = vc.MethodByName("Head") - } - } else { - method = vc.MethodByName("Head") - } + //exec main logic + method = vc.MethodByName(runMethod) + method.Call(in) - method.Call(in) - } else if r.Method == "DELETE" || (r.Method == "POST" && r.Form.Get("_method") == "delete") { - if runrouter.hasMethod { - if m, ok := runrouter.methods["delete"]; ok { - method = vc.MethodByName(m) - } else if m, ok = runrouter.methods["*"]; ok { - method = vc.MethodByName(m) - } else { - method = vc.MethodByName("Delete") - } - } else { - method = vc.MethodByName("Delete") - } - method.Call(in) - } else if r.Method == "PUT" || (r.Method == "POST" && r.Form.Get("_method") == "put") { - if runrouter.hasMethod { - if m, ok := runrouter.methods["put"]; ok { - method = vc.MethodByName(m) - } else if m, ok = runrouter.methods["*"]; ok { - method = vc.MethodByName(m) - } else { - method = vc.MethodByName("Put") - } - } else { - method = vc.MethodByName("Put") - } - method.Call(in) - } else if r.Method == "POST" { - if runrouter.hasMethod { - if m, ok := runrouter.methods["post"]; ok { - method = vc.MethodByName(m) - } else if m, ok = runrouter.methods["*"]; ok { - method = vc.MethodByName(m) - } else { - method = vc.MethodByName("Post") - } - } else { - method = vc.MethodByName("Post") - } - method.Call(in) - } else if r.Method == "PATCH" { - if runrouter.hasMethod { - if m, ok := runrouter.methods["patch"]; ok { - method = vc.MethodByName(m) - } else if m, ok = runrouter.methods["*"]; ok { - method = vc.MethodByName(m) - } else { - method = vc.MethodByName("Patch") - } - } else { - method = vc.MethodByName("Patch") - } - method.Call(in) - } else if r.Method == "OPTIONS" { - if runrouter.hasMethod { - if m, ok := runrouter.methods["options"]; ok { - method = vc.MethodByName(m) - } else if m, ok = runrouter.methods["*"]; ok { - method = vc.MethodByName(m) - } else { - method = vc.MethodByName("Options") - } - } else { - method = vc.MethodByName("Options") - } - method.Call(in) - } - gotofunc := vc.Elem().FieldByName("gotofunc").String() - if gotofunc != "" { - method = vc.MethodByName(gotofunc) - if method.IsValid() { - method.Call(in) - } else { - panic("gotofunc is exists:" + gotofunc) - } - } + //render template if !w.started && !context.Input.IsWebsocket() { if AutoRender { method = vc.MethodByName("Render") @@ -748,6 +703,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } } + // finish all runrouter. release resource method = vc.MethodByName("Finish") method.Call(in) @@ -757,105 +713,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } } - //start autorouter - - if p.enableAuto { - if !findrouter { - lastindex := strings.LastIndex(requestPath, "/") - lastsub := requestPath[lastindex+1:] - if subindex := strings.LastIndex(lastsub, "."); subindex != -1 { - context.Input.Params[":ext"] = lastsub[subindex+1:] - r.URL.Query().Add(":ext", lastsub[subindex+1:]) - r.URL.RawQuery = r.URL.Query().Encode() - requestPath = requestPath[:len(requestPath)-len(lastsub[subindex:])] - } - for cName, methodmap := range p.autoRouter { - - if strings.ToLower(requestPath) == "/"+cName { - http.Redirect(w, r, requestPath+"/", 301) - goto Admin - } - - if strings.ToLower(requestPath) == "/"+cName+"/" { - requestPath = requestPath + "index" - } - if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") { - for mName, controllerType := range methodmap { - if strings.ToLower(requestPath) == "/"+cName+"/"+strings.ToLower(mName) || - (strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) && - requestPath[len("/"+cName+"/"+strings.ToLower(mName)):len("/"+cName+"/"+strings.ToLower(mName))+1] == "/") { - if r.Method == "POST" { - r.ParseMultipartForm(MaxMemory) - } - // set find - findrouter = true - - //execute middleware filters - if do_filter(BeforeExec) { - goto Admin - } - - //parse params - otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):] - if len(otherurl) > 1 { - plist := strings.Split(otherurl, "/") - for k, v := range plist[1:] { - params[strconv.Itoa(k)] = v - } - } - //Invoke the request handler - vc := reflect.New(controllerType) - - //call the controller init function - init := vc.MethodByName("Init") - in := make([]reflect.Value, 3) - in[0] = reflect.ValueOf(context) - in[1] = reflect.ValueOf(controllerType.Name()) - in[2] = reflect.ValueOf(vc.Interface()) - init.Call(in) - //call prepare function - in = make([]reflect.Value, 0) - method := vc.MethodByName("Prepare") - method.Call(in) - method = vc.MethodByName(mName) - method.Call(in) - //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf - if EnableXSRF { - method = vc.MethodByName("XsrfToken") - method.Call(in) - if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" || - (r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) { - method = vc.MethodByName("CheckXsrfCookie") - method.Call(in) - } - } - if !w.started && !context.Input.IsWebsocket() { - if AutoRender { - method = vc.MethodByName("Render") - callMethodWithError(method, in) - } - } - method = vc.MethodByName("Finish") - method.Call(in) - - //execute middleware filters - if do_filter(AfterExec) { - goto Admin - } - - goto Admin - } - } - } - } - } - } - - //if no matches to url, throw a not found exception - if !findrouter { - middleware.Exception("404", rw, r, "") - } - Admin: do_filter(FinishRouter) @@ -864,7 +721,7 @@ Admin: timeend := time.Since(starttime) if FilterMonitorFunc(r.Method, requestPath, timeend) { if runrouter != nil { - go toolbox.StatisticsMap.AddStatistics(r.Method, requestPath, runrouter.controllerType.Name(), timeend) + go toolbox.StatisticsMap.AddStatistics(r.Method, requestPath, runrouter.Name(), timeend) } else { go toolbox.StatisticsMap.AddStatistics(r.Method, requestPath, "", timeend) } @@ -890,6 +747,21 @@ func (p *ControllerRegistor) getErrorHandler(errorCode string) func(rw http.Resp return handler } +func (p *ControllerRegistor) getRunMethod(method string, router *controllerInfo) string { + method = strings.ToLower(method) + if router.hasMethod { + if m, ok := router.methods[method]; ok { + return m + } else if m, ok = router.methods["*"]; ok { + return m + } else { + return strings.Title(method) + } + } else { + return strings.Title(method) + } +} + //responseWriter is a wrapper for the http.ResponseWriter //started set to true if response was written to then don't execute other handler type responseWriter struct {