From ead635e62ff0198b29d9a01cebfbae3a169ccaa4 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 18 Sep 2015 10:31:10 +0800 Subject: [PATCH 01/11] default exception handler --- error.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/error.go b/error.go index ff060c4d..ee21cee9 100644 --- a/error.go +++ b/error.go @@ -390,18 +390,19 @@ func ErrorController(c ControllerInterface) *App { // show error string as simple text message. // if error string is empty, show 503 or 500 error as default. func exception(errCode string, ctx *context.Context) { - code, _ := strconv.Atoi(errCode) - if code == 0 { - code = 503 - } for _, ec := range []string{errCode, "503", "500"} { + code, _ := strconv.Atoi(ec) + if code == 0 { + code = 503 + } if h, ok := ErrorMaps[ec]; ok { executeError(h, ctx, code) return } } //if 50x error has been removed from errorMap - ctx.ResponseWriter.WriteHeader(code) + //set 503 as default + ctx.ResponseWriter.WriteHeader(503) ctx.WriteString(errCode) } From 8df2cca627441eeb0a7a2723337a39479f88089b Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 18 Sep 2015 10:32:21 +0800 Subject: [PATCH 02/11] add comment --- error.go | 1 + 1 file changed, 1 insertion(+) diff --git a/error.go b/error.go index ee21cee9..1e047234 100644 --- a/error.go +++ b/error.go @@ -204,6 +204,7 @@ type errorInfo struct { } // map of http handlers for each error string. +// there is 10 kinds default error(40x and 50x) var ErrorMaps = make(map[string]*errorInfo, 10) // show 401 unauthorized error. From 2a96f33543a8a52902e2cfa64423d1998258611a Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 18 Sep 2015 10:36:16 +0800 Subject: [PATCH 03/11] more clean code --- beego.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/beego.go b/beego.go index 329e0713..7c0a7ca7 100644 --- a/beego.go +++ b/beego.go @@ -15,6 +15,7 @@ package beego import ( + "fmt" "os" "path/filepath" "strconv" @@ -45,14 +46,11 @@ func AddAPPStartHook(hf hookfunc) { func Run(params ...string) { initBeforeHTTPRun() - if len(params) > 0 && params[0] != "" { - strs := strings.Split(params[0], ":") - if len(strs) > 0 && strs[0] != "" { - HTTPAddr = strs[0] - } - if len(strs) > 1 && strs[1] != "" { - HTTPPort, _ = strconv.Atoi(strs[1]) - } + params = append(params, fmt.Sprintf("%s:%d", HTTPAddr, HTTPPort)) + addr := strings.Split(params[0], ":") + if len(addr) == 2 { + HTTPAddr = addr[0] + HTTPPort, _ = strconv.Atoi(addr[1]) } BeeApp.Run() From cc5abc6b306ec035316c266a6484fb7725cbec32 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 18 Sep 2015 17:03:00 +0800 Subject: [PATCH 04/11] default atoi func to handle exception --- error.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/error.go b/error.go index 1e047234..435c5d77 100644 --- a/error.go +++ b/error.go @@ -391,19 +391,22 @@ func ErrorController(c ControllerInterface) *App { // show error string as simple text message. // if error string is empty, show 503 or 500 error as default. func exception(errCode string, ctx *context.Context) { - for _, ec := range []string{errCode, "503", "500"} { - code, _ := strconv.Atoi(ec) - if code == 0 { - code = 503 + atoi := func(code string) int { + v, err := strconv.Atoi(code) + if err == nil { + return v } + return 503 + } + + for _, ec := range []string{errCode, "503", "500"} { if h, ok := ErrorMaps[ec]; ok { - executeError(h, ctx, code) + executeError(h, ctx, atoi(ec)) return } } //if 50x error has been removed from errorMap - //set 503 as default - ctx.ResponseWriter.WriteHeader(503) + ctx.ResponseWriter.WriteHeader(atoi(errCode)) ctx.WriteString(errCode) } From 0ac690d2c87496a18b4354972d68ac41e6865b69 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 18 Sep 2015 17:59:28 +0800 Subject: [PATCH 05/11] method name refactor --- error.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/error.go b/error.go index 435c5d77..2a1a861d 100644 --- a/error.go +++ b/error.go @@ -376,13 +376,14 @@ func ErrorController(c ControllerInterface) *App { rt := reflectVal.Type() ct := reflect.Indirect(reflectVal).Type() for i := 0; i < rt.NumMethod(); i++ { - if !utils.InSlice(rt.Method(i).Name, exceptMethod) && strings.HasPrefix(rt.Method(i).Name, "Error") { + methodName := rt.Method(i).Name + if !utils.InSlice(methodName, exceptMethod) && strings.HasPrefix(methodName, "Error") { errinfo := &errorInfo{} errinfo.errorType = errorTypeController errinfo.controllerType = ct - errinfo.method = rt.Method(i).Name - errname := strings.TrimPrefix(rt.Method(i).Name, "Error") - ErrorMaps[errname] = errinfo + errinfo.method = methodName + errName := strings.TrimPrefix(methodName, "Error") + ErrorMaps[errName] = errinfo } } return BeeApp From 40cb8e0cf14546c20418a65b2a1a290ac22229d0 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 18 Sep 2015 18:18:12 +0800 Subject: [PATCH 06/11] use reflect to ensure all methods been except --- router.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/router.go b/router.go index efb1a1f3..57addd4e 100644 --- a/router.go +++ b/router.go @@ -63,13 +63,14 @@ var ( "CONNECT": "CONNECT", } // these beego.Controller's methods shouldn't reflect to AutoRouter - exceptMethod = []string{"Init", "Prepare", "Finish", "Render", "RenderString", - "RenderBytes", "Redirect", "Abort", "StopRun", "UrlFor", "ServeJson", "ServeJsonp", - "ServeXml", "Input", "ParseForm", "GetString", "GetStrings", "GetInt", "GetBool", - "GetFloat", "GetFile", "SaveToFile", "StartSession", "SetSession", "GetSession", - "DelSession", "SessionRegenerateID", "DestroySession", "IsAjax", "GetSecureCookie", - "SetSecureCookie", "XsrfToken", "CheckXsrfCookie", "XsrfFormHtml", - "GetControllerAndAction"} + exceptMethod = func() []string { + methods := []string{} + rv := reflect.TypeOf(&Controller{}) + for i := 0; i < rv.NumMethod(); i++ { + methods = append(methods, rv.Method(i).Name) + } + return methods + }() urlPlaceholder = "{{placeholder}}" // DefaultAccessLogFilter will skip the accesslog if return true From 56032c67af2c343a04904d0bcc5f0bcc7c9d7422 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 18 Sep 2015 18:31:06 +0800 Subject: [PATCH 07/11] runFunction camel name --- router.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/router.go b/router.go index 57addd4e..d2f9a4b5 100644 --- a/router.go +++ b/router.go @@ -109,7 +109,7 @@ type controllerInfo struct { controllerType reflect.Type methods map[string]string handler http.Handler - runfunction FilterFunc + runFunction FilterFunc routerType int } @@ -325,7 +325,7 @@ func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) { route := &controllerInfo{} route.pattern = pattern route.routerType = routerTypeRESTFul - route.runfunction = f + route.runFunction = f methods := make(map[string]string) if method == "*" { for _, val := range HTTPMETHOD { @@ -726,7 +726,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) if routerInfo.routerType == routerTypeRESTFul { if _, ok := routerInfo.methods[r.Method]; ok { isRunable = true - routerInfo.runfunction(context) + routerInfo.runFunction(context) } else { exception("405", context) goto Admin From 983bac986a2eaff63b6910cef811048651337f6f Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 18 Sep 2015 18:34:07 +0800 Subject: [PATCH 08/11] runFunction camel name --- router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router.go b/router.go index d2f9a4b5..4f9d07e0 100644 --- a/router.go +++ b/router.go @@ -109,7 +109,7 @@ type controllerInfo struct { controllerType reflect.Type methods map[string]string handler http.Handler - runFunction FilterFunc + runFunction FilterFunc routerType int } From caf37144950796dd0ff924dff96e69077e15aacc Mon Sep 17 00:00:00 2001 From: JessonChan Date: Sat, 19 Sep 2015 05:41:10 +0800 Subject: [PATCH 09/11] revert exceptMethod --- router.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/router.go b/router.go index 4f9d07e0..c0a50988 100644 --- a/router.go +++ b/router.go @@ -63,14 +63,13 @@ var ( "CONNECT": "CONNECT", } // these beego.Controller's methods shouldn't reflect to AutoRouter - exceptMethod = func() []string { - methods := []string{} - rv := reflect.TypeOf(&Controller{}) - for i := 0; i < rv.NumMethod(); i++ { - methods = append(methods, rv.Method(i).Name) - } - return methods - }() + exceptMethod = []string{"Init", "Prepare", "Finish", "Render", "RenderString", + "RenderBytes", "Redirect", "Abort", "StopRun", "UrlFor", "ServeJson", "ServeJsonp", + "ServeXml", "Input", "ParseForm", "GetString", "GetStrings", "GetInt", "GetBool", + "GetFloat", "GetFile", "SaveToFile", "StartSession", "SetSession", "GetSession", + "DelSession", "SessionRegenerateID", "DestroySession", "IsAjax", "GetSecureCookie", + "SetSecureCookie", "XsrfToken", "CheckXsrfCookie", "XsrfFormHtml", + "GetControllerAndAction"} urlPlaceholder = "{{placeholder}}" // DefaultAccessLogFilter will skip the accesslog if return true From 69bee9ef3c94f901b84b643cb1b1bf785954fa6d Mon Sep 17 00:00:00 2001 From: JessonChan Date: Sat, 19 Sep 2015 05:52:52 +0800 Subject: [PATCH 10/11] // beego.Run("localhost") --- beego.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/beego.go b/beego.go index 7c0a7ca7..bcbf0496 100644 --- a/beego.go +++ b/beego.go @@ -15,11 +15,11 @@ package beego import ( - "fmt" "os" "path/filepath" "strconv" "strings" + "net/http" ) // beego web framework version. @@ -41,16 +41,20 @@ func AddAPPStartHook(hf hookfunc) { // Run beego application. // beego.Run() default run on HttpPort +// beego.Run("localhost") // beego.Run(":8089") // beego.Run("127.0.0.1:8089") func Run(params ...string) { initBeforeHTTPRun() - params = append(params, fmt.Sprintf("%s:%d", HTTPAddr, HTTPPort)) - addr := strings.Split(params[0], ":") - if len(addr) == 2 { - HTTPAddr = addr[0] - HTTPPort, _ = strconv.Atoi(addr[1]) + if len(params) > 0 && params[0] != "" { + strs := strings.Split(params[0], ":") + if len(strs) > 0 && strs[0] != "" { + HTTPAddr = strs[0] + } + if len(strs) > 1 && strs[1] != "" { + HTTPPort, _ = strconv.Atoi(strs[1]) + } } BeeApp.Run() From 07a424581db160732347b33f0b65cd1dfd9d9b40 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Sat, 19 Sep 2015 05:53:28 +0800 Subject: [PATCH 11/11] // beego.Run("localhost") --- beego.go | 1 - 1 file changed, 1 deletion(-) diff --git a/beego.go b/beego.go index bcbf0496..6c964d5a 100644 --- a/beego.go +++ b/beego.go @@ -19,7 +19,6 @@ import ( "path/filepath" "strconv" "strings" - "net/http" ) // beego web framework version.