mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
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>
This commit is contained in:
82
modules/templates/base.go
Normal file
82
modules/templates/base.go
Normal file
@@ -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
|
||||
}
|
@@ -9,7 +9,9 @@ package templates
|
||||
import (
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
texttmpl "text/template"
|
||||
|
||||
@@ -25,6 +27,25 @@ var (
|
||||
bodyTemplates = template.New("")
|
||||
)
|
||||
|
||||
// GetAsset returns asset content via name
|
||||
func GetAsset(name string) ([]byte, error) {
|
||||
bs, err := ioutil.ReadFile(filepath.Join(setting.CustomPath, name))
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
return bs, nil
|
||||
}
|
||||
|
||||
return ioutil.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...)
|
||||
}
|
||||
|
||||
// HTMLRenderer implements the macaron handler for serving HTML templates.
|
||||
func HTMLRenderer() macaron.Handler {
|
||||
return macaron.Renderer(macaron.RenderOptions{
|
||||
|
@@ -12,7 +12,9 @@ import (
|
||||
"html/template"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
texttmpl "text/template"
|
||||
|
||||
@@ -46,6 +48,30 @@ func (templates templateFileSystem) Get(name string) (io.Reader, error) {
|
||||
return nil, fmt.Errorf("file '%s' not found", name)
|
||||
}
|
||||
|
||||
// GetAsset get a special asset, only for chi
|
||||
func GetAsset(name string) ([]byte, error) {
|
||||
bs, err := ioutil.ReadFile(filepath.Join(setting.CustomPath, name))
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
return bs, nil
|
||||
}
|
||||
return Asset(strings.TrimPrefix(name, "templates/"))
|
||||
}
|
||||
|
||||
// GetAssetNames only for chi
|
||||
func GetAssetNames() []string {
|
||||
realFS := Assets.(vfsgen۰FS)
|
||||
var tmpls = make([]string, 0, len(realFS))
|
||||
for k := range realFS {
|
||||
tmpls = append(tmpls, "templates/"+k[1:])
|
||||
}
|
||||
|
||||
customDir := path.Join(setting.CustomPath, "templates")
|
||||
customTmpls := getDirAssetNames(customDir)
|
||||
return append(tmpls, customTmpls...)
|
||||
}
|
||||
|
||||
func NewTemplateFileSystem() templateFileSystem {
|
||||
fs := templateFileSystem{}
|
||||
fs.files = make([]macaron.TemplateFile, 0, 10)
|
||||
|
Reference in New Issue
Block a user