From 1d264a6545a3d7a58745804d0b7ffb02abd533ab Mon Sep 17 00:00:00 2001 From: John Date: Mon, 16 Sep 2019 10:44:07 +0800 Subject: [PATCH] add package gmode for release mode management; add error returning for template rendoring for gmvc.View/ghttp.Response --- frame/gmvc/view.go | 30 +++++++----- net/ghttp/ghttp_response_view.go | 17 +++++-- util/gmode/gmode.go | 80 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 util/gmode/gmode.go diff --git a/frame/gmvc/view.go b/frame/gmvc/view.go index 4c1d14045..5317a6480 100644 --- a/frame/gmvc/view.go +++ b/frame/gmvc/view.go @@ -9,6 +9,8 @@ package gmvc import ( "sync" + "github.com/gogf/gf/util/gmode" + "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/gview" ) @@ -91,22 +93,28 @@ func (view *View) BindFuncMap(funcMap gview.FuncMap) { } // 解析并显示指定模板 -func (view *View) Display(file ...string) { +func (view *View) Display(file ...string) error { name := "index.tpl" if len(file) > 0 { name = file[0] } if content, err := view.Parse(name); err != nil { - view.response.Write("Tpl Parsing Error: " + err.Error()) - } else { - view.response.Write(content) - } -} - -// 解析并显示模板内容 -func (view *View) DisplayContent(content string) error { - if content, err := view.ParseContent(content); err != nil { - view.response.Write("Tpl Parsing Error: " + err.Error()) + if gmode.IsDevelop() { + view.response.Write("Tpl Parsing Error: " + err.Error()) + } + return err + } else { + view.response.Write(content) + } + return nil +} + +// 解析并显示模板内容 +func (view *View) DisplayContent(content string) error { + if content, err := view.ParseContent(content); err != nil { + if gmode.IsDevelop() { + view.response.Write("Tpl Parsing Error: " + err.Error()) + } return err } else { view.response.Write(content) diff --git a/net/ghttp/ghttp_response_view.go b/net/ghttp/ghttp_response_view.go index 8b42a6833..e22f36cf3 100644 --- a/net/ghttp/ghttp_response_view.go +++ b/net/ghttp/ghttp_response_view.go @@ -10,24 +10,33 @@ package ghttp import ( "github.com/gogf/gf/frame/gins" "github.com/gogf/gf/os/gview" + "github.com/gogf/gf/util/gmode" ) // 展示模板,可以给定模板参数,及临时的自定义模板函数 -func (r *Response) WriteTpl(tpl string, params ...gview.Params) { +func (r *Response) WriteTpl(tpl string, params ...gview.Params) error { if b, err := r.ParseTpl(tpl, params...); err != nil { - r.Write("Template Parsing Error: " + err.Error()) + if gmode.IsDevelop() { + r.Write("Template Parsing Error: " + err.Error()) + } + return err } else { r.Write(b) } + return nil } // 展示模板内容,可以给定模板参数,及临时的自定义模板函数 -func (r *Response) WriteTplContent(content string, params ...gview.Params) { +func (r *Response) WriteTplContent(content string, params ...gview.Params) error { if b, err := r.ParseTplContent(content, params...); err != nil { - r.Write("Template Parsing Error: " + err.Error()) + if gmode.IsDevelop() { + r.Write("Template Parsing Error: " + err.Error()) + } + return err } else { r.Write(b) } + return nil } // 解析模板文件,并返回模板内容 diff --git a/util/gmode/gmode.go b/util/gmode/gmode.go new file mode 100644 index 000000000..2276f55c3 --- /dev/null +++ b/util/gmode/gmode.go @@ -0,0 +1,80 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// Package gmode provides release mode management for project. +package gmode + +import "github.com/gogf/gf/os/gfile" + +const ( + NOT_SET = 0 + DEVELOP = 1 + TESTING = 2 + STAGING = 3 + PRODUCT = 4 +) + +var ( + currentMode = NOT_SET +) + +// Set sets the mode for current application. +func Set(mode int) { + currentMode = mode +} + +// SetDevelop sets current mode DEVELOP for current application. +func SetDevelop() { + Set(DEVELOP) +} + +// SetTesting sets current mode TESTING for current application. +func SetTesting() { + Set(TESTING) +} + +// SetStaging sets current mode STAGING for current application. +func SetStaging() { + Set(STAGING) +} + +// SetProduct sets current mode PRODUCT for current application. +func SetProduct() { + Set(PRODUCT) +} + +// Mode returns current application mode set. +func Mode() int { + // If current mode is not set, do this auto check. + if currentMode == NOT_SET { + if gfile.MainPkgPath() != "" { + return DEVELOP + } else { + return PRODUCT + } + } + return currentMode +} + +// IsDevelop checks and returns whether current application is running in DEVELOP mode. +func IsDevelop() bool { + return Mode() == DEVELOP +} + +// IsTesting checks and returns whether current application is running in TESTING mode. +func IsTesting() bool { + return Mode() == TESTING +} + +// IsStaging checks and returns whether current application is running in STAGING mode. +func IsStaging() bool { + return Mode() == STAGING +} + +// IsProduct checks and returns whether current application is running in PRODUCT mode. +func IsProduct() bool { + return Mode() == PRODUCT +}