summaryrefslogtreecommitdiffstats
path: root/modules/templates/base.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-01-05 21:05:40 +0800
committerGitHub <noreply@github.com>2021-01-05 21:05:40 +0800
commit15a475b7dbcf7923d9518dff7764b20e404eb774 (patch)
tree8789f1f82c5e41345b442df4e58120bdd5f8bade /modules/templates/base.go
parent126c9331d6d8789563fae5d5bac2196d63fee0e8 (diff)
downloadgitea-15a475b7dbcf7923d9518dff7764b20e404eb774.tar.gz
gitea-15a475b7dbcf7923d9518dff7764b20e404eb774.zip
Fix recovery middleware to render gitea style page. (#13857)
* Some changes to fix recovery * Move Recovery to middlewares * Remove trace code * Fix lint * add session middleware and remove dependent on macaron for sso * Fix panic 500 page rendering * Fix bugs * Fix fmt * Fix vendor * recover unnecessary change * Fix lint and addd some comments about the copied codes. * Use util.StatDir instead of com.StatDir Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'modules/templates/base.go')
-rw-r--r--modules/templates/base.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/modules/templates/base.go b/modules/templates/base.go
new file mode 100644
index 0000000000..a9b6b2737c
--- /dev/null
+++ b/modules/templates/base.go
@@ -0,0 +1,82 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package templates
+
+import (
+ "os"
+ "strings"
+ "time"
+
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/util"
+)
+
+// Vars represents variables to be render in golang templates
+type Vars map[string]interface{}
+
+// Merge merges another vars to the current, another Vars will override the current
+func (vars Vars) Merge(another map[string]interface{}) Vars {
+ for k, v := range another {
+ vars[k] = v
+ }
+ return vars
+}
+
+// BaseVars returns all basic vars
+func BaseVars() Vars {
+ var startTime = time.Now()
+ return map[string]interface{}{
+ "IsLandingPageHome": setting.LandingPageURL == setting.LandingPageHome,
+ "IsLandingPageExplore": setting.LandingPageURL == setting.LandingPageExplore,
+ "IsLandingPageOrganizations": setting.LandingPageURL == setting.LandingPageOrganizations,
+
+ "ShowRegistrationButton": setting.Service.ShowRegistrationButton,
+ "ShowMilestonesDashboardPage": setting.Service.ShowMilestonesDashboardPage,
+ "ShowFooterBranding": setting.ShowFooterBranding,
+ "ShowFooterVersion": setting.ShowFooterVersion,
+
+ "EnableSwagger": setting.API.EnableSwagger,
+ "EnableOpenIDSignIn": setting.Service.EnableOpenIDSignIn,
+ "PageStartTime": startTime,
+ "TmplLoadTimes": func() string {
+ return time.Since(startTime).String()
+ },
+ }
+}
+
+func getDirAssetNames(dir string) []string {
+ var tmpls []string
+ f, err := os.Stat(dir)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return tmpls
+ }
+ log.Warn("Unable to check if templates dir %s is a directory. Error: %v", dir, err)
+ return tmpls
+ }
+ if !f.IsDir() {
+ log.Warn("Templates dir %s is a not directory.", dir)
+ return tmpls
+ }
+
+ files, err := util.StatDir(dir)
+ if err != nil {
+ log.Warn("Failed to read %s templates dir. %v", dir, err)
+ return tmpls
+ }
+ for _, filePath := range files {
+ if strings.HasPrefix(filePath, "mail/") {
+ continue
+ }
+
+ if !strings.HasSuffix(filePath, ".tmpl") {
+ continue
+ }
+
+ tmpls = append(tmpls, "templates/"+filePath)
+ }
+ return tmpls
+}