mirror of
1
Fork 0

[bugfix] Fix panic when relative tmpl path given to router (#528)

This commit is contained in:
tobi 2022-05-02 16:06:03 +02:00 committed by GitHub
parent 9265a09a65
commit 3112949b6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 8 deletions

View File

@ -34,21 +34,20 @@ import (
// LoadTemplates loads html templates for use by the given engine // LoadTemplates loads html templates for use by the given engine
func loadTemplates(engine *gin.Engine) error { func loadTemplates(engine *gin.Engine) error {
templateBaseDir := viper.GetString(config.Keys.WebTemplateBaseDir) templateBaseDir := viper.GetString(config.Keys.WebTemplateBaseDir)
if templateBaseDir == "" {
if !filepath.IsAbs(templateBaseDir) { return fmt.Errorf("%s cannot be empty and must be a relative or absolute path", config.Keys.WebTemplateBaseDir)
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current working directory: %w", err)
} }
templateBaseDir = filepath.Join(cwd, viper.GetString(config.Keys.WebTemplateBaseDir)) templateBaseDir, err := filepath.Abs(templateBaseDir)
if err != nil {
return fmt.Errorf("error getting absolute path of %s: %s", templateBaseDir, err)
} }
if _, err := os.Stat(filepath.Join(templateBaseDir, "index.tmpl")); err != nil { if _, err := os.Stat(filepath.Join(templateBaseDir, "index.tmpl")); err != nil {
return fmt.Errorf("%s doesn't seem to contain the templates; index.tmpl is missing: %w", templateBaseDir, err) return fmt.Errorf("%s doesn't seem to contain the templates; index.tmpl is missing: %w", templateBaseDir, err)
} }
engine.LoadHTMLGlob(fmt.Sprintf("%s*", templateBaseDir)) engine.LoadHTMLGlob(filepath.Join(templateBaseDir, "*"))
return nil return nil
} }