Skip to content

Commit f494776

Browse files
authored
Use a variable but a function for IsProd because of a slight performance increment (#17368)
1 parent 0208ea0 commit f494776

File tree

12 files changed

+19
-22
lines changed

12 files changed

+19
-22
lines changed

cmd/serv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func fail(userMessage, logMessage string, args ...interface{}) error {
8080
fmt.Fprintln(os.Stderr, "Gitea:", userMessage)
8181

8282
if len(logMessage) > 0 {
83-
if !setting.IsProd() {
83+
if !setting.IsProd {
8484
fmt.Fprintf(os.Stderr, logMessage+"\n", args...)
8585
}
8686
}

models/db/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ func NewTestEngine() (err error) {
136136
}
137137

138138
x.SetMapper(names.GonicMapper{})
139-
x.SetLogger(NewXORMLogger(!setting.IsProd()))
140-
x.ShowSQL(!setting.IsProd())
139+
x.SetLogger(NewXORMLogger(!setting.IsProd))
140+
x.ShowSQL(!setting.IsProd)
141141
return syncTables()
142142
}
143143

modules/context/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
9595
if status == http.StatusInternalServerError {
9696
log.ErrorWithSkip(1, "%s: %s", title, message)
9797

98-
if setting.IsProd() && !(ctx.User != nil && ctx.User.IsAdmin) {
98+
if setting.IsProd && !(ctx.User != nil && ctx.User.IsAdmin) {
9999
message = ""
100100
}
101101
}
@@ -112,7 +112,7 @@ func (ctx *APIContext) InternalServerError(err error) {
112112
log.ErrorWithSkip(1, "InternalServerError: %v", err)
113113

114114
var message string
115-
if !setting.IsProd() || (ctx.User != nil && ctx.User.IsAdmin) {
115+
if !setting.IsProd || (ctx.User != nil && ctx.User.IsAdmin) {
116116
message = err.Error()
117117
}
118118

modules/context/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (ctx *Context) NotFound(title string, err error) {
225225
func (ctx *Context) notFoundInternal(title string, err error) {
226226
if err != nil {
227227
log.ErrorWithSkip(2, "%s: %v", title, err)
228-
if !setting.IsProd() {
228+
if !setting.IsProd {
229229
ctx.Data["ErrorMsg"] = err
230230
}
231231
}
@@ -261,7 +261,7 @@ func (ctx *Context) ServerError(title string, err error) {
261261
func (ctx *Context) serverErrorInternal(title string, err error) {
262262
if err != nil {
263263
log.ErrorWithSkip(2, "%s: %v", title, err)
264-
if !setting.IsProd() {
264+
if !setting.IsProd {
265265
ctx.Data["ErrorMsg"] = err
266266
}
267267
}
@@ -645,7 +645,7 @@ func Contexter() func(next http.Handler) http.Handler {
645645
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
646646
"PageStartTime": startTime,
647647
"Link": link,
648-
"IsProd": setting.IsProd(),
648+
"IsProd": setting.IsProd,
649649
},
650650
}
651651
// PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules

modules/httpcache/httpcache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
// AddCacheControlToHeader adds suitable cache-control headers to response
2020
func AddCacheControlToHeader(h http.Header, d time.Duration) {
21-
if setting.IsProd() {
21+
if setting.IsProd {
2222
h.Set("Cache-Control", "private, max-age="+strconv.Itoa(int(d.Seconds())))
2323
} else {
2424
h.Set("Cache-Control", "no-store")

modules/setting/setting.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,17 +419,13 @@ var (
419419
PIDFile = "/run/gitea.pid"
420420
WritePIDFile bool
421421
RunMode string
422+
IsProd bool
422423
RunUser string
423424
IsWindows bool
424425
HasRobotsTxt bool
425426
InternalToken string // internal access token
426427
)
427428

428-
// IsProd if it's a production mode
429-
func IsProd() bool {
430-
return strings.EqualFold(RunMode, "prod")
431-
}
432-
433429
func getAppPath() (string, error) {
434430
var appPath string
435431
var err error
@@ -906,6 +902,7 @@ func NewContext() {
906902
// Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly.
907903
unsafeAllowRunAsRoot := Cfg.Section("").Key("I_AM_BEING_UNSAFE_RUNNING_AS_ROOT").MustBool(false)
908904
RunMode = Cfg.Section("").Key("RUN_MODE").MustString("prod")
905+
IsProd = strings.EqualFold(RunMode, "prod")
909906
// Does not check run user when the install lock is off.
910907
if InstallLock {
911908
currentUser, match := IsRunUserMatchCurrentUser(RunUser)

modules/templates/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func HTMLRenderer() *render.Render {
9191
Funcs: NewFuncMap(),
9292
Asset: GetAsset,
9393
AssetNames: GetAssetNames,
94-
IsDevelopment: !setting.IsProd(),
94+
IsDevelopment: !setting.IsProd,
9595
DisableHTTPErrorRendering: true,
9696
})
9797
}

routers/common/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func Middlewares() []func(http.Handler) http.Handler {
6262
if err := recover(); err != nil {
6363
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2)))
6464
log.Error("%v", combinedErr)
65-
if setting.IsProd() {
65+
if setting.IsProd {
6666
http.Error(resp, http.StatusText(500), 500)
6767
} else {
6868
http.Error(resp, combinedErr, 500)

routers/install/routes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func installRecovery() func(next http.Handler) http.Handler {
4040
if err := recover(); err != nil {
4141
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2)))
4242
log.Error(combinedErr)
43-
if setting.IsProd() {
43+
if setting.IsProd {
4444
http.Error(w, http.StatusText(500), 500)
4545
} else {
4646
http.Error(w, combinedErr, 500)
@@ -63,7 +63,7 @@ func installRecovery() func(next http.Handler) http.Handler {
6363

6464
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
6565

66-
if !setting.IsProd() {
66+
if !setting.IsProd {
6767
store["ErrorMsg"] = combinedErr
6868
}
6969
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store))

routers/web/base.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func Recovery() func(next http.Handler) http.Handler {
131131

132132
sessionStore := session.GetSession(req)
133133
if sessionStore == nil {
134-
if setting.IsProd() {
134+
if setting.IsProd {
135135
http.Error(w, http.StatusText(500), 500)
136136
} else {
137137
http.Error(w, combinedErr, 500)
@@ -164,7 +164,7 @@ func Recovery() func(next http.Handler) http.Handler {
164164

165165
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
166166

167-
if !setting.IsProd() {
167+
if !setting.IsProd {
168168
store["ErrorMsg"] = combinedErr
169169
}
170170
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store))

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func RegisterRoutes(m *web.Route) {
477477
m.Post("/action/{action}", user.Action)
478478
}, reqSignIn)
479479

480-
if !setting.IsProd() {
480+
if !setting.IsProd {
481481
m.Get("/template/*", dev.TemplatePreview)
482482
}
483483

services/auth/sspi_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (s *SSPI) Init() error {
6464
Funcs: templates.NewFuncMap(),
6565
Asset: templates.GetAsset,
6666
AssetNames: templates.GetAssetNames,
67-
IsDevelopment: !setting.IsProd(),
67+
IsDevelopment: !setting.IsProd,
6868
})
6969
return nil
7070
}

0 commit comments

Comments
 (0)