From 5df8c8fdd1b9f6b543b6618e31d5f4cbde0abbfa Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 20 Oct 2021 22:37:19 +0800 Subject: [PATCH] Use a variable but a function for IsProd because of a slight performance increment (#17368) --- cmd/serv.go | 2 +- models/db/engine.go | 4 ++-- modules/context/api.go | 4 ++-- modules/context/context.go | 6 +++--- modules/httpcache/httpcache.go | 2 +- modules/setting/setting.go | 7 ++----- modules/templates/base.go | 2 +- routers/common/middleware.go | 2 +- routers/install/routes.go | 4 ++-- routers/web/base.go | 4 ++-- routers/web/web.go | 2 +- services/auth/sspi_windows.go | 2 +- 12 files changed, 19 insertions(+), 22 deletions(-) diff --git a/cmd/serv.go b/cmd/serv.go index 2173a3a38b858..13c27f1ac05dd 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -80,7 +80,7 @@ func fail(userMessage, logMessage string, args ...interface{}) error { fmt.Fprintln(os.Stderr, "Gitea:", userMessage) if len(logMessage) > 0 { - if !setting.IsProd() { + if !setting.IsProd { fmt.Fprintf(os.Stderr, logMessage+"\n", args...) } } diff --git a/models/db/engine.go b/models/db/engine.go index 256eb2f3fc725..78b4ac22dde85 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -136,8 +136,8 @@ func NewTestEngine() (err error) { } x.SetMapper(names.GonicMapper{}) - x.SetLogger(NewXORMLogger(!setting.IsProd())) - x.ShowSQL(!setting.IsProd()) + x.SetLogger(NewXORMLogger(!setting.IsProd)) + x.ShowSQL(!setting.IsProd) return syncTables() } diff --git a/modules/context/api.go b/modules/context/api.go index e5216d911f8a6..c978835af8749 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -95,7 +95,7 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) { if status == http.StatusInternalServerError { log.ErrorWithSkip(1, "%s: %s", title, message) - if setting.IsProd() && !(ctx.User != nil && ctx.User.IsAdmin) { + if setting.IsProd && !(ctx.User != nil && ctx.User.IsAdmin) { message = "" } } @@ -112,7 +112,7 @@ func (ctx *APIContext) InternalServerError(err error) { log.ErrorWithSkip(1, "InternalServerError: %v", err) var message string - if !setting.IsProd() || (ctx.User != nil && ctx.User.IsAdmin) { + if !setting.IsProd || (ctx.User != nil && ctx.User.IsAdmin) { message = err.Error() } diff --git a/modules/context/context.go b/modules/context/context.go index 0a603cced5f53..383a69ad6332f 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -225,7 +225,7 @@ func (ctx *Context) NotFound(title string, err error) { func (ctx *Context) notFoundInternal(title string, err error) { if err != nil { log.ErrorWithSkip(2, "%s: %v", title, err) - if !setting.IsProd() { + if !setting.IsProd { ctx.Data["ErrorMsg"] = err } } @@ -261,7 +261,7 @@ func (ctx *Context) ServerError(title string, err error) { func (ctx *Context) serverErrorInternal(title string, err error) { if err != nil { log.ErrorWithSkip(2, "%s: %v", title, err) - if !setting.IsProd() { + if !setting.IsProd { ctx.Data["ErrorMsg"] = err } } @@ -645,7 +645,7 @@ func Contexter() func(next http.Handler) http.Handler { "CurrentURL": setting.AppSubURL + req.URL.RequestURI(), "PageStartTime": startTime, "Link": link, - "IsProd": setting.IsProd(), + "IsProd": setting.IsProd, }, } // PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules diff --git a/modules/httpcache/httpcache.go b/modules/httpcache/httpcache.go index 35d4e6dfd82e8..11b63148d9d74 100644 --- a/modules/httpcache/httpcache.go +++ b/modules/httpcache/httpcache.go @@ -18,7 +18,7 @@ import ( // AddCacheControlToHeader adds suitable cache-control headers to response func AddCacheControlToHeader(h http.Header, d time.Duration) { - if setting.IsProd() { + if setting.IsProd { h.Set("Cache-Control", "private, max-age="+strconv.Itoa(int(d.Seconds()))) } else { h.Set("Cache-Control", "no-store") diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 2133184cfc40d..a1ac090e46073 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -419,17 +419,13 @@ var ( PIDFile = "/run/gitea.pid" WritePIDFile bool RunMode string + IsProd bool RunUser string IsWindows bool HasRobotsTxt bool InternalToken string // internal access token ) -// IsProd if it's a production mode -func IsProd() bool { - return strings.EqualFold(RunMode, "prod") -} - func getAppPath() (string, error) { var appPath string var err error @@ -906,6 +902,7 @@ func NewContext() { // Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly. unsafeAllowRunAsRoot := Cfg.Section("").Key("I_AM_BEING_UNSAFE_RUNNING_AS_ROOT").MustBool(false) RunMode = Cfg.Section("").Key("RUN_MODE").MustString("prod") + IsProd = strings.EqualFold(RunMode, "prod") // Does not check run user when the install lock is off. if InstallLock { currentUser, match := IsRunUserMatchCurrentUser(RunUser) diff --git a/modules/templates/base.go b/modules/templates/base.go index cb83143bd3424..f753bfbe4ea40 100644 --- a/modules/templates/base.go +++ b/modules/templates/base.go @@ -91,7 +91,7 @@ func HTMLRenderer() *render.Render { Funcs: NewFuncMap(), Asset: GetAsset, AssetNames: GetAssetNames, - IsDevelopment: !setting.IsProd(), + IsDevelopment: !setting.IsProd, DisableHTTPErrorRendering: true, }) } diff --git a/routers/common/middleware.go b/routers/common/middleware.go index 1d96522dd9d19..7c5c72f5cc38a 100644 --- a/routers/common/middleware.go +++ b/routers/common/middleware.go @@ -62,7 +62,7 @@ func Middlewares() []func(http.Handler) http.Handler { if err := recover(); err != nil { combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2))) log.Error("%v", combinedErr) - if setting.IsProd() { + if setting.IsProd { http.Error(resp, http.StatusText(500), 500) } else { http.Error(resp, combinedErr, 500) diff --git a/routers/install/routes.go b/routers/install/routes.go index e9aca85d8edd6..ad0003a9e8e34 100644 --- a/routers/install/routes.go +++ b/routers/install/routes.go @@ -40,7 +40,7 @@ func installRecovery() func(next http.Handler) http.Handler { if err := recover(); err != nil { combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2))) log.Error(combinedErr) - if setting.IsProd() { + if setting.IsProd { http.Error(w, http.StatusText(500), 500) } else { http.Error(w, combinedErr, 500) @@ -63,7 +63,7 @@ func installRecovery() func(next http.Handler) http.Handler { w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions) - if !setting.IsProd() { + if !setting.IsProd { store["ErrorMsg"] = combinedErr } err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store)) diff --git a/routers/web/base.go b/routers/web/base.go index f50c5229b1d23..16d3192da21dc 100644 --- a/routers/web/base.go +++ b/routers/web/base.go @@ -131,7 +131,7 @@ func Recovery() func(next http.Handler) http.Handler { sessionStore := session.GetSession(req) if sessionStore == nil { - if setting.IsProd() { + if setting.IsProd { http.Error(w, http.StatusText(500), 500) } else { http.Error(w, combinedErr, 500) @@ -164,7 +164,7 @@ func Recovery() func(next http.Handler) http.Handler { w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions) - if !setting.IsProd() { + if !setting.IsProd { store["ErrorMsg"] = combinedErr } err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store)) diff --git a/routers/web/web.go b/routers/web/web.go index caec0676714c4..88565d6d85563 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -477,7 +477,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/action/{action}", user.Action) }, reqSignIn) - if !setting.IsProd() { + if !setting.IsProd { m.Get("/template/*", dev.TemplatePreview) } diff --git a/services/auth/sspi_windows.go b/services/auth/sspi_windows.go index 346e9439884f9..821a3df459c32 100644 --- a/services/auth/sspi_windows.go +++ b/services/auth/sspi_windows.go @@ -64,7 +64,7 @@ func (s *SSPI) Init() error { Funcs: templates.NewFuncMap(), Asset: templates.GetAsset, AssetNames: templates.GetAssetNames, - IsDevelopment: !setting.IsProd(), + IsDevelopment: !setting.IsProd, }) return nil }