From ad81d0ce7c278b9d8a16a2af19443e8368661d19 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Mon, 19 Apr 2021 23:37:05 +0800 Subject: [PATCH] init exceptMethod --- CHANGELOG.md | 1 + server/web/hooks.go | 9 ++++----- server/web/router.go | 27 +++++++++++++-------------- server/web/router_test.go | 6 ++++-- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dbe15d0..fb51c215 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - Feature issue #4402 finish router get example. [4416](https://github.com/beego/beego/pull/4416) - Proposal: Add Bind() method for `web.Controller` [4491](https://github.com/beego/beego/issues/4579) - Optimize AddAutoPrefix: only register one router in case-insensitive mode. [4582](https://github.com/beego/beego/pull/4582) +- Init exceptMethod by using reflection. [4583](https://github.com/beego/beego/pull/4583) ## Fix Sonar - [4473](https://github.com/beego/beego/pull/4473) diff --git a/server/web/hooks.go b/server/web/hooks.go index 0f72e711..6176586e 100644 --- a/server/web/hooks.go +++ b/server/web/hooks.go @@ -2,13 +2,12 @@ package web import ( "encoding/json" - "mime" - "net/http" - "path/filepath" - "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/server/web/context" "github.com/beego/beego/v2/server/web/session" + "mime" + "net/http" + "path/filepath" ) // register MIME type with content type @@ -95,4 +94,4 @@ func registerGzip() error { ) } return nil -} +} \ No newline at end of file diff --git a/server/web/router.go b/server/web/router.go index 7a578e4b..0f6db6de 100644 --- a/server/web/router.go +++ b/server/web/router.go @@ -69,20 +69,8 @@ var ( "UNLOCK": true, } // these web.Controller's methods shouldn't reflect to AutoRouter - exceptMethod = []string{"Abort", "CheckXSRFCookie", "CustomAbort", "DelSession", - "DestroySession", "Finish", "GetBool", "GetControllerAndAction", - "GetFile", "GetFiles", "GetFloat", "GetInt", "GetInt16", - "GetInt32", "GetInt64", "GetInt8", "GetSecureCookie", "GetSession", - "GetString", "GetStrings", "GetUint16", "GetUint32", "GetUint64", - "GetUint8", "HandlerFunc", "Init", "Input", - "IsAjax", "Mapping", "ParseForm", - "Prepare", "Redirect", "Render", "RenderBytes", - "RenderString", "SaveToFile", "SaveToFileWithBuffer", "SaveToFileWithBuffer", - "ServeFormatted", "ServeJSON", "ServeJSONP", "ServeXML", "ServeYAML", - "SessionRegenerateID", "SetData", "SetSecureCookie", "SetSession", "StartSession", - "StopRun", "URLFor", "URLMapping", "XSRFFormHTML", - "XSRFToken", - } + // see registerControllerExceptMethods + exceptMethod = initExceptMethod() urlPlaceholder = "{{placeholder}}" // DefaultAccessLogFilter will skip the accesslog if return true @@ -116,6 +104,17 @@ func ExceptMethodAppend(action string) { exceptMethod = append(exceptMethod, action) } +func initExceptMethod() []string { + res := make([]string, 0, 32) + c := &Controller{} + t := reflect.TypeOf(c) + for i := 0; i < t.NumMethod(); i++ { + m := t.Method(i) + res = append(res, m.Name) + } + return res +} + // ControllerInfo holds information about the controller. type ControllerInfo struct { pattern string diff --git a/server/web/router_test.go b/server/web/router_test.go index 3633aee7..e8b823d2 100644 --- a/server/web/router_test.go +++ b/server/web/router_test.go @@ -137,10 +137,12 @@ func TestUrlFor(t *testing.T) { func TestUrlFor3(t *testing.T) { handler := NewControllerRegister() handler.AddAuto(&TestController{}) - if a := handler.URLFor("TestController.Myext"); a != "/test/myext" && a != "/Test/Myext" { + a := handler.URLFor("TestController.Myext") + if a != "/test/myext" && a != "/Test/Myext" { t.Errorf("TestController.Myext must equal to /test/myext, but get " + a) } - if a := handler.URLFor("TestController.GetURL"); a != "/test/geturl" && a != "/Test/GetURL" { + a = handler.URLFor("TestController.GetURL") + if a != "/test/geturl" && a != "/Test/GetURL" { t.Errorf("TestController.GetURL must equal to /test/geturl, but get " + a) } }