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

Merge pull request #1519 from vvelikodny/develop

Refactoring: Move dev & prod runmodes to const
This commit is contained in:
astaxie 2015-12-30 21:03:54 +08:00
commit 9519fc6c96
7 changed files with 23 additions and 17 deletions

View File

@ -24,8 +24,14 @@ import (
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
) )
const (
// beego web framework version. // beego web framework version.
const VERSION = "1.5.0" VERSION = "1.5.0"
// beego run modes
DEV = "dev"
PROD = "prod"
)
//hook function to run //hook function to run
type hookfunc func() error type hookfunc func() error

View File

@ -111,7 +111,7 @@ var (
func init() { func init() {
BConfig = &BeegoConfig{ BConfig = &BeegoConfig{
AppName: "beego", AppName: "beego",
RunMode: "dev", RunMode: DEV,
RouterCaseSensitive: true, RouterCaseSensitive: true,
ServerName: "beegoServer:" + VERSION, ServerName: "beegoServer:" + VERSION,
RecoverPanic: true, RecoverPanic: true,

View File

@ -204,7 +204,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt
} }
if BConfig.RunMode == "dev" { if BConfig.RunMode == DEV {
buildFiles := []string{c.TplNames} buildFiles := []string{c.TplNames}
if c.LayoutSections != nil { if c.LayoutSections != nil {
for _, sectionTpl := range c.LayoutSections { for _, sectionTpl := range c.LayoutSections {
@ -255,7 +255,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
if c.TplNames == "" { if c.TplNames == "" {
c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt 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) BuildTemplate(BConfig.WebConfig.ViewsPath, c.TplNames)
} }
if _, ok := BeeTemplates[c.TplNames]; !ok { if _, ok := BeeTemplates[c.TplNames]; !ok {
@ -319,7 +319,7 @@ func (c *Controller) ServeJSON(encoding ...bool) {
hasIndent = true hasIndent = true
hasEncoding = false hasEncoding = false
) )
if BConfig.RunMode == "prod" { if BConfig.RunMode == PROD {
hasIndent = false hasIndent = false
} }
if len(encoding) > 0 && encoding[0] == true { if len(encoding) > 0 && encoding[0] == true {
@ -331,7 +331,7 @@ func (c *Controller) ServeJSON(encoding ...bool) {
// ServeJSONP sends a jsonp response. // ServeJSONP sends a jsonp response.
func (c *Controller) ServeJSONP() { func (c *Controller) ServeJSONP() {
hasIndent := true hasIndent := true
if BConfig.RunMode == "prod" { if BConfig.RunMode == PROD {
hasIndent = false hasIndent = false
} }
c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent) c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent)
@ -340,7 +340,7 @@ func (c *Controller) ServeJSONP() {
// ServeXML sends xml response. // ServeXML sends xml response.
func (c *Controller) ServeXML() { func (c *Controller) ServeXML() {
hasIndent := true hasIndent := true
if BConfig.RunMode == "prod" { if BConfig.RunMode == PROD {
hasIndent = false hasIndent = false
} }
c.Ctx.Output.XML(c.Data["xml"], hasIndent) c.Ctx.Output.XML(c.Data["xml"], hasIndent)

View File

@ -70,7 +70,7 @@ func registerSession() error {
func registerTemplate() error { func registerTemplate() error {
if BConfig.WebConfig.AutoRender { if BConfig.WebConfig.AutoRender {
if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil { if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil {
if BConfig.RunMode == "dev" { if BConfig.RunMode == DEV {
Warn(err) Warn(err)
} }
return err return err

View File

@ -220,7 +220,7 @@ func Test_Preflight(t *testing.T) {
func Benchmark_WithoutCORS(b *testing.B) { func Benchmark_WithoutCORS(b *testing.B) {
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
handler := beego.NewControllerRegister() handler := beego.NewControllerRegister()
beego.BConfig.RunMode = "prod" beego.BConfig.RunMode = beego.PROD
handler.Any("/foo", func(ctx *context.Context) { handler.Any("/foo", func(ctx *context.Context) {
ctx.Output.SetStatus(500) ctx.Output.SetStatus(500)
}) })
@ -234,7 +234,7 @@ func Benchmark_WithoutCORS(b *testing.B) {
func Benchmark_WithCORS(b *testing.B) { func Benchmark_WithCORS(b *testing.B) {
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
handler := beego.NewControllerRegister() handler := beego.NewControllerRegister()
beego.BConfig.RunMode = "prod" beego.BConfig.RunMode = beego.PROD
handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{ handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{
AllowAllOrigins: true, AllowAllOrigins: true,
AllowCredentials: true, AllowCredentials: true,

View File

@ -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 only when the Runmode is dev will generate router file in the router/auto.go from the controller
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{}) // Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
func (p *ControllerRegister) Include(cList ...ControllerInterface) { func (p *ControllerRegister) Include(cList ...ControllerInterface) {
if BConfig.RunMode == "dev" { if BConfig.RunMode == DEV {
skip := make(map[string]bool, 10) skip := make(map[string]bool, 10)
for _, c := range cList { for _, c := range cList {
reflectVal := reflect.ValueOf(c) 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.pool.Put(context)
defer p.recoverPanic(context) defer p.recoverPanic(context)
if BConfig.RunMode == "dev" { if BConfig.RunMode == DEV {
context.Output.Header("Server", BConfig.ServerName) 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 var devinfo string
if findrouter { if findrouter {
if routerInfo != nil { if routerInfo != nil {
@ -862,7 +862,7 @@ func (p *ControllerRegister) recoverPanic(context *beecontext.Context) {
Critical(fmt.Sprintf("%s:%d", file, line)) Critical(fmt.Sprintf("%s:%d", file, line))
stack = stack + fmt.Sprintln(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) showErr(err, context, stack)
} }
} }

View File

@ -49,7 +49,7 @@ func serverStaticRouter(ctx *context.Context) {
} }
if filePath == "" || fileInfo == nil { if filePath == "" || fileInfo == nil {
if BConfig.RunMode == "dev" { if BConfig.RunMode == DEV {
Warn("Can't find/open the file:", filePath, err) Warn("Can't find/open the file:", filePath, err)
} }
http.NotFound(ctx.ResponseWriter, ctx.Request) http.NotFound(ctx.ResponseWriter, ctx.Request)
@ -68,7 +68,7 @@ func serverStaticRouter(ctx *context.Context) {
} }
b, n, sch, err := openFile(filePath, fileInfo, acceptEncoding) b, n, sch, err := openFile(filePath, fileInfo, acceptEncoding)
if err != nil { if err != nil {
if BConfig.RunMode == "dev" { if BConfig.RunMode == DEV {
Warn("Can't compress the file:", filePath, err) Warn("Can't compress the file:", filePath, err)
} }
http.NotFound(ctx.ResponseWriter, ctx.Request) http.NotFound(ctx.ResponseWriter, ctx.Request)