From 64b40c3d82eeff06dd40265cb9d9d714cd999428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Tue, 23 Jan 2024 19:01:59 -0800 Subject: [PATCH] clean up models move template data functions to their own package --- internal/models/templatedata.go | 49 ++------------------------------ internal/render/render.go | 5 ++-- internal/td/td.go | 50 +++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 internal/td/td.go diff --git a/internal/models/templatedata.go b/internal/models/templatedata.go index 47d0523..a738ab6 100644 --- a/internal/models/templatedata.go +++ b/internal/models/templatedata.go @@ -1,20 +1,8 @@ package models -import ( - "errors" - "html/template" - "os" - "strings" - "time" - - "github.com/ilyakaznacheev/cleanenv" -) - -const dataDir string = "./templates/data/" - -// in order of precedence -var dataExtensions = [4]string{"yml", "yaml", "toml", "json"} +import "html/template" +// Link holds an http link, text to display, and an iconify icon class type Link struct { Href, Icon template.URL Text string @@ -32,36 +20,3 @@ type TemplateData struct { Warning string `json:"Warning" yaml:"Warning" toml:"Warning"` Error string `json:"Error" yaml:"Error" toml:"Error"` } - -// makeBasicTemplateData creates a blank TemplateData containing only the -// time the related template was generated -func MakeBasicTemplateData(when time.Time) TemplateData { - strMap := map[string]string{ - "GeneratedAt": when.Format(time.UnixDate), - } - - templateData := TemplateData{ - StringMap: strMap, - } - return templateData -} - -// LoadTemplateData loads template data from file. If that -// fails, it returns an empty TemplateData and an error -func LoadTemplateData(page string) (TemplateData, error) { - var data TemplateData - output := dataDir + strings.ReplaceAll(page, "tmpl", "") - - for _, extension := range dataExtensions { - if info, err := os.Stat(output + extension); err == nil && !info.IsDir() { - err = cleanenv.ReadConfig(output+extension, &data) - if err == nil { - // don't try anymore files - return data, nil - } - } - } - - // couldn't load anything from file - return TemplateData{}, errors.New("Couldn't load data from file") -} diff --git a/internal/render/render.go b/internal/render/render.go index 28b0d7a..66aec35 100644 --- a/internal/render/render.go +++ b/internal/render/render.go @@ -10,6 +10,7 @@ import ( "git.burning.moe/celediel/burning.moe/internal/config" "git.burning.moe/celediel/burning.moe/internal/models" + "git.burning.moe/celediel/burning.moe/internal/td" ) const ( @@ -103,7 +104,7 @@ func RenderTemplate(w http.ResponseWriter, filename string) { } // Get template data from file, or generate simple - data, err := models.LoadTemplateData(filename) + data, err := td.LoadTemplateData(filename) if err == nil { app.Logger.Debug(fmt.Sprintf("Loaded data for template %s.", filename), "data", data) if _, ok := data.StringMap["GeneratedAt"]; !ok { @@ -111,7 +112,7 @@ func RenderTemplate(w http.ResponseWriter, filename string) { } } else { app.Logger.Info(fmt.Sprintf("Loading template data for %s failed, using default template data.", filename), "err", err) - data = models.MakeBasicTemplateData(template.GeneratedAt) + data = td.MakeBasicTemplateData(template.GeneratedAt) } // Execute templates in a new buffer diff --git a/internal/td/td.go b/internal/td/td.go new file mode 100644 index 0000000..3cef8bf --- /dev/null +++ b/internal/td/td.go @@ -0,0 +1,50 @@ +package td + +import ( + "errors" + "os" + "strings" + "time" + + "git.burning.moe/celediel/burning.moe/internal/models" + + "github.com/ilyakaznacheev/cleanenv" +) + +const dataDir string = "./templates/data/" + +// in order of precedence +var dataExtensions = [4]string{"yml", "yaml", "toml", "json"} + +// makeBasicTemplateData creates a blank TemplateData containing only the +// time the related template was generated +func MakeBasicTemplateData(when time.Time) models.TemplateData { + strMap := map[string]string{ + "GeneratedAt": when.Format(time.UnixDate), + } + + templateData := models.TemplateData{ + StringMap: strMap, + } + return templateData +} + +// LoadTemplateData loads template data from file. If that +// fails, it returns an empty TemplateData and an error +func LoadTemplateData(page string) (models.TemplateData, error) { + var data models.TemplateData + output := dataDir + strings.ReplaceAll(page, "tmpl", "") + + for _, extension := range dataExtensions { + if info, err := os.Stat(output + extension); err == nil && !info.IsDir() { + err = cleanenv.ReadConfig(output+extension, &data) + if err == nil { + // don't try anymore files + return data, nil + } + } + } + + // couldn't load anything from file + return models.TemplateData{}, errors.New("Couldn't load data from file") +}