From 48fd9675ad9467588b3bd582489601b71cae7c4a Mon Sep 17 00:00:00 2001 From: Vitaly Velikodny Date: Tue, 29 Dec 2015 21:32:37 +0300 Subject: [PATCH] Refactoring: Move dev & prod runmodes to const --- beego.go | 10 ++++++++-- config.go | 2 +- config/ini_test.go | 4 +++- config/json_test.go | 4 +++- config/xml/xml_test.go | 3 ++- config/yaml/yaml_test.go | 3 ++- controller.go | 10 +++++----- hooks.go | 2 +- plugins/cors/cors_test.go | 4 ++-- router.go | 8 ++++---- staticfile.go | 4 ++-- 11 files changed, 33 insertions(+), 21 deletions(-) diff --git a/beego.go b/beego.go index a4d996e3..fd4600b7 100644 --- a/beego.go +++ b/beego.go @@ -24,8 +24,14 @@ import ( "github.com/astaxie/beego/logs" ) -// beego web framework version. -const VERSION = "1.5.0" +const ( + // beego web framework version. + VERSION = "1.5.0" + + // beego run modes + DEV = "dev" + PROD = "prod" +) //hook function to run type hookfunc func() error diff --git a/config.go b/config.go index bb01e25f..3a95d6fd 100644 --- a/config.go +++ b/config.go @@ -111,7 +111,7 @@ var ( func init() { BConfig = &BeegoConfig{ AppName: "beego", - RunMode: "dev", + RunMode: DEV, RouterCaseSensitive: true, ServerName: "beegoServer:" + VERSION, RecoverPanic: true, diff --git a/config/ini_test.go b/config/ini_test.go index 7599ab8b..38093e0e 100644 --- a/config/ini_test.go +++ b/config/ini_test.go @@ -17,6 +17,8 @@ package config import ( "os" "testing" + + "github.com/astaxie/beego" ) var inicontext = ` @@ -67,7 +69,7 @@ func TestIni(t *testing.T) { t.Error(pi) t.Fatal(err) } - if iniconf.String("runmode") != "dev" { + if iniconf.String("runmode") != beego.DEV { t.Fatal("runmode not equal to dev") } if v, err := iniconf.Bool("autorender"); err != nil || v != false { diff --git a/config/json_test.go b/config/json_test.go index 5aedae36..ca4dfffd 100644 --- a/config/json_test.go +++ b/config/json_test.go @@ -17,6 +17,8 @@ package config import ( "os" "testing" + + "github.com/astaxie/beego" ) var jsoncontext = `{ @@ -120,7 +122,7 @@ func TestJson(t *testing.T) { t.Error(pi) t.Fatal(err) } - if jsonconf.String("runmode") != "dev" { + if jsonconf.String("runmode") != beego.DEV { t.Fatal("runmode not equal to dev") } if v := jsonconf.Strings("unknown"); len(v) > 0 { diff --git a/config/xml/xml_test.go b/config/xml/xml_test.go index fa3c17f1..721a85fd 100644 --- a/config/xml/xml_test.go +++ b/config/xml/xml_test.go @@ -18,6 +18,7 @@ import ( "os" "testing" + "github.com/astaxie/beego" "github.com/astaxie/beego/config" ) @@ -65,7 +66,7 @@ func TestXML(t *testing.T) { t.Error(pi) t.Fatal(err) } - if xmlconf.String("runmode") != "dev" { + if xmlconf.String("runmode") != beego.DEV { t.Fatal("runmode not equal to dev") } if v, err := xmlconf.Bool("autorender"); err != nil || v != false { diff --git a/config/yaml/yaml_test.go b/config/yaml/yaml_test.go index 19ecdca1..09cde8fd 100644 --- a/config/yaml/yaml_test.go +++ b/config/yaml/yaml_test.go @@ -18,6 +18,7 @@ import ( "os" "testing" + "github.com/astaxie/beego" "github.com/astaxie/beego/config" ) @@ -62,7 +63,7 @@ func TestYaml(t *testing.T) { t.Error(pi) t.Fatal(err) } - if yamlconf.String("runmode") != "dev" { + if yamlconf.String("runmode") != beego.DEV { t.Fatal("runmode not equal to dev") } if v, err := yamlconf.Bool("autorender"); err != nil || v != false { diff --git a/controller.go b/controller.go index a5bb0607..1d0d9025 100644 --- a/controller.go +++ b/controller.go @@ -204,7 +204,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt } - if BConfig.RunMode == "dev" { + if BConfig.RunMode == DEV { buildFiles := []string{c.TplNames} if c.LayoutSections != nil { for _, sectionTpl := range c.LayoutSections { @@ -255,7 +255,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { if c.TplNames == "" { c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt } - if BConfig.RunMode == "dev" { + if BConfig.RunMode == DEV { BuildTemplate(BConfig.WebConfig.ViewsPath, c.TplNames) } if _, ok := BeeTemplates[c.TplNames]; !ok { @@ -319,7 +319,7 @@ func (c *Controller) ServeJSON(encoding ...bool) { hasIndent = true hasEncoding = false ) - if BConfig.RunMode == "prod" { + if BConfig.RunMode == PROD { hasIndent = false } if len(encoding) > 0 && encoding[0] == true { @@ -331,7 +331,7 @@ func (c *Controller) ServeJSON(encoding ...bool) { // ServeJSONP sends a jsonp response. func (c *Controller) ServeJSONP() { hasIndent := true - if BConfig.RunMode == "prod" { + if BConfig.RunMode == PROD { hasIndent = false } c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent) @@ -340,7 +340,7 @@ func (c *Controller) ServeJSONP() { // ServeXML sends xml response. func (c *Controller) ServeXML() { hasIndent := true - if BConfig.RunMode == "prod" { + if BConfig.RunMode == PROD { hasIndent = false } c.Ctx.Output.XML(c.Data["xml"], hasIndent) diff --git a/hooks.go b/hooks.go index 0588206f..53b298a0 100644 --- a/hooks.go +++ b/hooks.go @@ -70,7 +70,7 @@ func registerSession() error { func registerTemplate() error { if BConfig.WebConfig.AutoRender { if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil { - if BConfig.RunMode == "dev" { + if BConfig.RunMode == DEV { Warn(err) } return err diff --git a/plugins/cors/cors_test.go b/plugins/cors/cors_test.go index 84d21cd0..34039143 100644 --- a/plugins/cors/cors_test.go +++ b/plugins/cors/cors_test.go @@ -220,7 +220,7 @@ func Test_Preflight(t *testing.T) { func Benchmark_WithoutCORS(b *testing.B) { recorder := httptest.NewRecorder() handler := beego.NewControllerRegister() - beego.BConfig.RunMode = "prod" + beego.BConfig.RunMode = beego.PROD handler.Any("/foo", func(ctx *context.Context) { ctx.Output.SetStatus(500) }) @@ -234,7 +234,7 @@ func Benchmark_WithoutCORS(b *testing.B) { func Benchmark_WithCORS(b *testing.B) { recorder := httptest.NewRecorder() handler := beego.NewControllerRegister() - beego.BConfig.RunMode = "prod" + beego.BConfig.RunMode = beego.PROD handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{ AllowAllOrigins: true, AllowCredentials: true, diff --git a/router.go b/router.go index 8d724ac6..69464ac2 100644 --- a/router.go +++ b/router.go @@ -204,7 +204,7 @@ func (p *ControllerRegister) addToRouter(method, pattern string, r *controllerIn // Include only when the Runmode is dev will generate router file in the router/auto.go from the controller // Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{}) func (p *ControllerRegister) Include(cList ...ControllerInterface) { - if BConfig.RunMode == "dev" { + if BConfig.RunMode == DEV { skip := make(map[string]bool, 10) for _, c := range cList { reflectVal := reflect.ValueOf(c) @@ -609,7 +609,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) defer p.pool.Put(context) defer p.recoverPanic(context) - if BConfig.RunMode == "dev" { + if BConfig.RunMode == DEV { context.Output.Header("Server", BConfig.ServerName) } @@ -815,7 +815,7 @@ Admin: } } - if BConfig.RunMode == "dev" || BConfig.Log.AccessLogs { + if BConfig.RunMode == DEV || BConfig.Log.AccessLogs { var devinfo string if findrouter { if routerInfo != nil { @@ -862,7 +862,7 @@ func (p *ControllerRegister) recoverPanic(context *beecontext.Context) { Critical(fmt.Sprintf("%s:%d", file, line)) stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line)) } - if BConfig.RunMode == "dev" { + if BConfig.RunMode == DEV { showErr(err, context, stack) } } diff --git a/staticfile.go b/staticfile.go index f3ea59a9..f9f3dc3e 100644 --- a/staticfile.go +++ b/staticfile.go @@ -49,7 +49,7 @@ func serverStaticRouter(ctx *context.Context) { } if filePath == "" || fileInfo == nil { - if BConfig.RunMode == "dev" { + if BConfig.RunMode == DEV { Warn("Can't find/open the file:", filePath, err) } http.NotFound(ctx.ResponseWriter, ctx.Request) @@ -68,7 +68,7 @@ func serverStaticRouter(ctx *context.Context) { } b, n, sch, err := openFile(filePath, fileInfo, acceptEncoding) if err != nil { - if BConfig.RunMode == "dev" { + if BConfig.RunMode == DEV { Warn("Can't compress the file:", filePath, err) } http.NotFound(ctx.ResponseWriter, ctx.Request)