Skip to content

Commit 5f39285

Browse files
authored
Improve RunMode / dev mode (#24886)
1. non-dev mode is treated as prod mode, to protect users from accidentally running in dev mode if there is a typo in this value. 2. in dev mode, do not need to really exit if there are template errors, because the template errors could be fixed by developer soon and the templates get reloaded, help: * #24845 (comment) 3. Fine tune the mail template loading message.
1 parent 694b38b commit 5f39285

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ APP_NAME = ; Gitea: Git with a cup of tea
4949
;; RUN_USER will automatically detect the current user - but you can set it here change it if you run locally
5050
RUN_USER = ; git
5151
;;
52-
;; Application run mode, affects performance and debugging. Either "dev", "prod" or "test", default is "prod"
53-
RUN_MODE = ; prod
52+
;; Application run mode, affects performance and debugging: "dev" or "prod", default is "prod"
53+
;; Mode "dev" makes Gitea easier to develop and debug, values other than "dev" are treated as "prod" which is for production use.
54+
;RUN_MODE = prod
5455

5556
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5657
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/administration/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ In addition there is _`StaticRootPath`_ which can be set as a built-in at build
7373
- `RUN_USER`: **_current OS username_/`$USER`/`$USERNAME` e.g. git**: The user Gitea will run as.
7474
This should be a dedicated system (non-user) account. Setting this incorrectly will cause Gitea
7575
to not start.
76-
- `RUN_MODE`: **prod**: Application run mode, affects performance and debugging. Either "dev", "prod" or "test".
76+
- `RUN_MODE`: **prod**: Application run mode, affects performance and debugging: `dev` or `prod`, default is `prod`. Mode `dev` makes Gitea easier to develop and debug, values other than `dev` are treated as `prod` which is for production use.
7777

7878
## Repository (`repository`)
7979

modules/setting/setting.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,13 @@ func loadRunModeFrom(rootCfg ConfigProvider) {
251251
if RunMode == "" {
252252
RunMode = rootSec.Key("RUN_MODE").MustString("prod")
253253
}
254-
IsProd = strings.EqualFold(RunMode, "prod")
254+
255+
// non-dev mode is treated as prod mode, to protect users from accidentally running in dev mode if there is a typo in this value.
256+
RunMode = strings.ToLower(RunMode)
257+
if RunMode != "dev" {
258+
RunMode = "prod"
259+
}
260+
IsProd = RunMode != "dev"
255261

256262
// check if we run as root
257263
if os.Getuid() == 0 {

modules/templates/htmlrenderer.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func HTMLRenderer() *HTMLRender {
9797
}
9898

9999
func ReloadHTMLTemplates() error {
100+
log.Trace("Reloading HTML templates")
100101
if err := htmlRender.CompileTemplates(); err != nil {
101102
log.Error("Template error: %v\n%s", err, log.Stack(2))
102103
return err
@@ -114,11 +115,11 @@ func initHTMLRenderer() {
114115
htmlRender = &HTMLRender{}
115116
if err := htmlRender.CompileTemplates(); err != nil {
116117
p := &templateErrorPrettier{assets: AssetFS()}
117-
wrapFatal(p.handleFuncNotDefinedError(err))
118-
wrapFatal(p.handleUnexpectedOperandError(err))
119-
wrapFatal(p.handleExpectedEndError(err))
120-
wrapFatal(p.handleGenericTemplateError(err))
121-
log.Fatal("HTMLRenderer CompileTemplates error: %v", err)
118+
wrapTmplErrMsg(p.handleFuncNotDefinedError(err))
119+
wrapTmplErrMsg(p.handleUnexpectedOperandError(err))
120+
wrapTmplErrMsg(p.handleExpectedEndError(err))
121+
wrapTmplErrMsg(p.handleGenericTemplateError(err))
122+
wrapTmplErrMsg(fmt.Sprintf("CompileTemplates error: %v", err))
122123
}
123124

124125
if !setting.IsProd {
@@ -128,11 +129,17 @@ func initHTMLRenderer() {
128129
}
129130
}
130131

131-
func wrapFatal(msg string) {
132+
func wrapTmplErrMsg(msg string) {
132133
if msg == "" {
133134
return
134135
}
135-
log.Fatal("Unable to compile templates, %s", msg)
136+
if setting.IsProd {
137+
// in prod mode, Gitea must have correct templates to run
138+
log.Fatal("Gitea can't run with template errors: %s", msg)
139+
} else {
140+
// in dev mode, do not need to really exit, because the template errors could be fixed by developer soon and the templates get reloaded
141+
log.Error("There are template errors but Gitea continues to run in dev mode: %s", msg)
142+
}
136143
}
137144

138145
type templateErrorPrettier struct {

modules/templates/mailer.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
6161
bodyTemplates.Funcs(NewFuncMap())
6262

6363
assetFS := AssetFS()
64-
refreshTemplates := func() {
64+
refreshTemplates := func(firstRun bool) {
65+
if !firstRun {
66+
log.Trace("Reloading mail templates")
67+
}
6568
assetPaths, err := ListMailTemplateAssetNames(assetFS)
6669
if err != nil {
6770
log.Error("Failed to list mail templates: %v", err)
@@ -75,17 +78,21 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
7578
continue
7679
}
7780
tmplName := strings.TrimPrefix(strings.TrimSuffix(assetPath, ".tmpl"), "mail/")
78-
log.Trace("Adding mail template %s: %s by %s", tmplName, assetPath, layerName)
81+
if firstRun {
82+
log.Trace("Adding mail template %s: %s by %s", tmplName, assetPath, layerName)
83+
}
7984
buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, tmplName, content)
8085
}
8186
}
8287

83-
refreshTemplates()
88+
refreshTemplates(true)
8489

8590
if !setting.IsProd {
8691
// Now subjectTemplates and bodyTemplates are both synchronized
8792
// thus it is safe to call refresh from a different goroutine
88-
go assetFS.WatchLocalChanges(ctx, refreshTemplates)
93+
go assetFS.WatchLocalChanges(ctx, func() {
94+
refreshTemplates(false)
95+
})
8996
}
9097

9198
return subjectTemplates, bodyTemplates

0 commit comments

Comments
 (0)