mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-01 09:51:39 -05:00
Make bindata static build parse builtin templates correctly (#24003)
Close #24002 Two problems before: 1. The `log.Fatal` is missing after these `wrapFatal` calls, so the error is not shown to users. 2. `GetTemplateAssetNames` has different behaviors for local files and builtin assets, for builtin assets, it also returns directories, so we need to check the extension again. I have tested with `TAGS="bindata sqlite sqlite_unlock_notify" make build && ./gitea` , it works well now. Before, the server responds internal server error (because it doesn't complete the template parsing)
This commit is contained in:
parent
fdbd646113
commit
94fde46151
1 changed files with 11 additions and 10 deletions
|
@ -67,10 +67,14 @@ func (h *HTMLRender) TemplateLookup(name string) (*template.Template, error) {
|
||||||
|
|
||||||
func (h *HTMLRender) CompileTemplates() error {
|
func (h *HTMLRender) CompileTemplates() error {
|
||||||
dirPrefix := "templates/"
|
dirPrefix := "templates/"
|
||||||
|
extSuffix := ".tmpl"
|
||||||
tmpls := template.New("")
|
tmpls := template.New("")
|
||||||
for _, path := range GetTemplateAssetNames() {
|
for _, path := range GetTemplateAssetNames() {
|
||||||
name := path[len(dirPrefix):]
|
if !strings.HasSuffix(path, extSuffix) {
|
||||||
name = strings.TrimSuffix(name, ".tmpl")
|
continue
|
||||||
|
}
|
||||||
|
name := strings.TrimPrefix(path, dirPrefix)
|
||||||
|
name = strings.TrimSuffix(name, extSuffix)
|
||||||
tmpl := tmpls.New(filepath.ToSlash(name))
|
tmpl := tmpls.New(filepath.ToSlash(name))
|
||||||
for _, fm := range NewFuncMap() {
|
for _, fm := range NewFuncMap() {
|
||||||
tmpl.Funcs(fm)
|
tmpl.Funcs(fm)
|
||||||
|
@ -101,7 +105,11 @@ func HTMLRenderer(ctx context.Context) (context.Context, *HTMLRender) {
|
||||||
|
|
||||||
renderer := &HTMLRender{}
|
renderer := &HTMLRender{}
|
||||||
if err := renderer.CompileTemplates(); err != nil {
|
if err := renderer.CompileTemplates(); err != nil {
|
||||||
handleFatalError(err)
|
wrapFatal(handleNotDefinedPanicError(err))
|
||||||
|
wrapFatal(handleUnexpected(err))
|
||||||
|
wrapFatal(handleExpectedEnd(err))
|
||||||
|
wrapFatal(handleGenericTemplateError(err))
|
||||||
|
log.Fatal("HTMLRenderer error: %v", err)
|
||||||
}
|
}
|
||||||
if !setting.IsProd {
|
if !setting.IsProd {
|
||||||
watcher.CreateWatcher(ctx, "HTML Templates", &watcher.CreateWatcherOpts{
|
watcher.CreateWatcher(ctx, "HTML Templates", &watcher.CreateWatcherOpts{
|
||||||
|
@ -116,13 +124,6 @@ func HTMLRenderer(ctx context.Context) (context.Context, *HTMLRender) {
|
||||||
return context.WithValue(ctx, rendererKey, renderer), renderer
|
return context.WithValue(ctx, rendererKey, renderer), renderer
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFatalError(err error) {
|
|
||||||
wrapFatal(handleNotDefinedPanicError(err))
|
|
||||||
wrapFatal(handleUnexpected(err))
|
|
||||||
wrapFatal(handleExpectedEnd(err))
|
|
||||||
wrapFatal(handleGenericTemplateError(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
func wrapFatal(format string, args []interface{}) {
|
func wrapFatal(format string, args []interface{}) {
|
||||||
if format == "" {
|
if format == "" {
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue