aboutsummaryrefslogtreecommitdiffstats
path: root/modules/templates/dynamic.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-08-28 10:43:25 +0100
committerGitHub <noreply@github.com>2022-08-28 10:43:25 +0100
commitbb0ff77e461ae080b1722701aea74010ff71e0ae (patch)
treed9550dc2f4342ca5babc4bf9191fa51aacdb922e /modules/templates/dynamic.go
parentc21d6511a8e0084644a53a3192f45443456b9a32 (diff)
downloadgitea-bb0ff77e461ae080b1722701aea74010ff71e0ae.tar.gz
gitea-bb0ff77e461ae080b1722701aea74010ff71e0ae.zip
Share HTML template renderers and create a watcher framework (#20218)
The recovery, API, Web and package frameworks all create their own HTML Renderers. This increases the memory requirements of Gitea unnecessarily with duplicate templates being kept in memory. Further the reloading framework in dev mode for these involves locking and recompiling all of the templates on each load. This will potentially hide concurrency issues and it is inefficient. This PR stores the templates renderer in the context and stores this context in the NormalRoutes, it then creates a fsnotify.Watcher framework to watch files. The watching framework is then extended to the mailer templates which were previously not being reloaded in dev. Then the locales are simplified to a similar structure. Fix #20210 Fix #20211 Fix #20217 Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/templates/dynamic.go')
-rw-r--r--modules/templates/dynamic.go100
1 files changed, 31 insertions, 69 deletions
diff --git a/modules/templates/dynamic.go b/modules/templates/dynamic.go
index de6968c314..4896580f62 100644
--- a/modules/templates/dynamic.go
+++ b/modules/templates/dynamic.go
@@ -8,15 +8,12 @@ package templates
import (
"html/template"
+ "io/fs"
"os"
- "path"
"path/filepath"
- "strings"
texttmpl "text/template"
- "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
- "code.gitea.io/gitea/modules/util"
)
var (
@@ -36,77 +33,42 @@ func GetAsset(name string) ([]byte, error) {
return os.ReadFile(filepath.Join(setting.StaticRootPath, name))
}
-// GetAssetNames returns assets list
-func GetAssetNames() []string {
- tmpls := getDirAssetNames(filepath.Join(setting.CustomPath, "templates"))
- tmpls2 := getDirAssetNames(filepath.Join(setting.StaticRootPath, "templates"))
- return append(tmpls, tmpls2...)
-}
-
-// Mailer provides the templates required for sending notification mails.
-func Mailer() (*texttmpl.Template, *template.Template) {
- for _, funcs := range NewTextFuncMap() {
- subjectTemplates.Funcs(funcs)
+// walkTemplateFiles calls a callback for each template asset
+func walkTemplateFiles(callback func(path, name string, d fs.DirEntry, err error) error) error {
+ if err := walkAssetDir(filepath.Join(setting.CustomPath, "templates"), true, callback); err != nil && !os.IsNotExist(err) {
+ return err
}
- for _, funcs := range NewFuncMap() {
- bodyTemplates.Funcs(funcs)
+ if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "templates"), true, callback); err != nil && !os.IsNotExist(err) {
+ return err
}
+ return nil
+}
- staticDir := path.Join(setting.StaticRootPath, "templates", "mail")
-
- isDir, err := util.IsDir(staticDir)
- if err != nil {
- log.Warn("Unable to check if templates dir %s is a directory. Error: %v", staticDir, err)
- }
- if isDir {
- files, err := util.StatDir(staticDir)
-
- if err != nil {
- log.Warn("Failed to read %s templates dir. %v", staticDir, err)
- } else {
- for _, filePath := range files {
- if !strings.HasSuffix(filePath, ".tmpl") {
- continue
- }
-
- content, err := os.ReadFile(path.Join(staticDir, filePath))
- if err != nil {
- log.Warn("Failed to read static %s template. %v", filePath, err)
- continue
- }
+// GetTemplateAssetNames returns list of template names
+func GetTemplateAssetNames() []string {
+ tmpls := getDirTemplateAssetNames(filepath.Join(setting.CustomPath, "templates"))
+ tmpls2 := getDirTemplateAssetNames(filepath.Join(setting.StaticRootPath, "templates"))
+ return append(tmpls, tmpls2...)
+}
- buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
- }
- }
+func walkMailerTemplates(callback func(path, name string, d fs.DirEntry, err error) error) error {
+ if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "templates", "mail"), false, callback); err != nil && !os.IsNotExist(err) {
+ return err
}
-
- customDir := path.Join(setting.CustomPath, "templates", "mail")
-
- isDir, err = util.IsDir(customDir)
- if err != nil {
- log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
+ if err := walkAssetDir(filepath.Join(setting.CustomPath, "templates", "mail"), false, callback); err != nil && !os.IsNotExist(err) {
+ return err
}
- if isDir {
- files, err := util.StatDir(customDir)
-
- if err != nil {
- log.Warn("Failed to read %s templates dir. %v", customDir, err)
- } else {
- for _, filePath := range files {
- if !strings.HasSuffix(filePath, ".tmpl") {
- continue
- }
-
- content, err := os.ReadFile(path.Join(customDir, filePath))
- if err != nil {
- log.Warn("Failed to read custom %s template. %v", filePath, err)
- continue
- }
+ return nil
+}
- buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
- }
- }
- }
+// BuiltinAsset will read the provided asset from the embedded assets
+// (This always returns os.ErrNotExist)
+func BuiltinAsset(name string) ([]byte, error) {
+ return nil, os.ErrNotExist
+}
- return subjectTemplates, bodyTemplates
+// BuiltinAssetNames returns the names of the embedded assets
+// (This always returns nil)
+func BuiltinAssetNames() []string {
+ return nil
}