From f82809bfb16d8c1450c52bcf158e8d9a65e8a978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Sat, 27 Jan 2024 16:27:51 -0800 Subject: [PATCH] un-export some functions they didn't need to be exported anyway --- internal/render/render.go | 167 +++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 83 deletions(-) diff --git a/internal/render/render.go b/internal/render/render.go index f7f2871..096efb7 100644 --- a/internal/render/render.go +++ b/internal/render/render.go @@ -26,15 +26,96 @@ func Initialise(a *config.AppConfig) { app = a if app.UseCache { var err error - app.TemplateCache, err = GenerateNewTemplateCache() + app.TemplateCache, err = generateNewTemplateCache() if err != nil { app.Logger.Fatal("Error generating template cache, bailing out!") } } } -// GenerateNewTemplateCache generates a new template cache. -func GenerateNewTemplateCache() (models.TemplateCache, error) { +// RenderTemplate renders requested template (t), pulling from cache. +func RenderTemplate(w http.ResponseWriter, filename string) { + // TODO: implement this better + if !app.UseCache { + regenerateTemplateCache() + } + + template, err := GetTemplateFromCache(filename) + if err != nil { + app.Logger.Fatalf("Tried loading %s from the cache, but %s!", filename, err) + } + + data, err := GetOrGenerateTemplateData(filename) + if err != nil { + app.Logger.Error(err) + } + + app.Logger.Debug(fmt.Sprintf("Executing template %s", filename), "data", &data) + err = template.Execute(data, w) + if err != nil { + app.Logger.Fatalf("Failed to execute template %s: %s", filename, err) + } +} + +func RenderTemplateWithData(w http.ResponseWriter, filename string, data *models.TemplateData) { + if !app.UseCache { + regenerateTemplateCache() + } + + template, err := GetTemplateFromCache(filename) + if err != nil { + app.Logger.Fatalf("Tried loading %s from the cache, but %s!", filename, err) + } + + app.Logger.Debug(fmt.Sprintf("Executing template %s", filename), "data", &data) + err = template.Execute(data, w) + if err != nil { + app.Logger.Fatalf("Failed to execute template %s: %s", filename, err) + } +} + +// GetTemplateFromCache gets templates from cache +func GetTemplateFromCache(filename string) (*models.TemplateCacheItem, error) { + if template, ok := app.TemplateCache.Cache[filename]; ok { + return &template, nil + } else { + return &models.TemplateCacheItem{}, errors.New("Couldn't load template from cache") + } +} + +// GetOrGenerateTemplateData gets template data from file, or generate simple +func GetOrGenerateTemplateData(filename string) (*models.TemplateData, error) { + template, err := GetTemplateFromCache(filename) + if err != nil { + return &models.TemplateData{}, err + } + + 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 { + data.StringMap["GeneratedAt"] = template.GeneratedAt.Format(time.UnixDate) + } + } else { + app.Logger.Info(fmt.Sprintf("Loading template data for %s failed, using default template data.", filename), "err", err) + data = td.MakeBasicTemplateData(template.GeneratedAt) + } + + return &data, nil +} + +// regenerateTemplateCache regenerates the template cache +func regenerateTemplateCache() { + c, err := generateNewTemplateCache() + if err != nil { + app.Logger.Fatal("Error generating template cache, bailing out!") + } + app.TemplateCache = c + +} + +// generateNewTemplateCache generates a new template cache. +func generateNewTemplateCache() (models.TemplateCache, error) { // start with an empty map cache := models.TemplateCache{} cache.Cache = map[string]models.TemplateCacheItem{} @@ -80,83 +161,3 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) { // All was good, so return the cache, and no error return cache, nil } - -// RenderTemplate renders requested template (t), pulling from cache. -func RenderTemplate(w http.ResponseWriter, filename string) { - // TODO: implement this better - if !app.UseCache { - RegenerateTemplateCache() - } - - template, err := GetTemplateFromCache(filename) - if err != nil { - app.Logger.Fatalf("Tried loading %s from the cache, but %s!", filename, err) - } - - data, err := GetOrGenerateTemplateData(filename) - if err != nil { - app.Logger.Error(err) - } - - app.Logger.Debug(fmt.Sprintf("Executing template %s", filename), "data", &data) - err = template.Execute(data, w) - if err != nil { - app.Logger.Fatalf("Failed to execute template %s: %s", filename, err) - } -} - -func RenderTemplateWithData(w http.ResponseWriter, filename string, data *models.TemplateData) { - if !app.UseCache { - RegenerateTemplateCache() - } - - template, err := GetTemplateFromCache(filename) - if err != nil { - app.Logger.Fatalf("Tried loading %s from the cache, but %s!", filename, err) - } - - app.Logger.Debug(fmt.Sprintf("Executing template %s", filename), "data", &data) - err = template.Execute(data, w) - if err != nil { - app.Logger.Fatalf("Failed to execute template %s: %s", filename, err) - } -} - -func RegenerateTemplateCache() { - c, err := GenerateNewTemplateCache() - if err != nil { - app.Logger.Fatal("Error generating template cache, bailing out!") - } - app.TemplateCache = c - -} - -// GetTemplateFromCache gets templates from cache -func GetTemplateFromCache(filename string) (*models.TemplateCacheItem, error) { - if template, ok := app.TemplateCache.Cache[filename]; ok { - return &template, nil - } else { - return &models.TemplateCacheItem{}, errors.New("Couldn't load template from cache") - } -} - -// GetOrGenerateTemplateData gets template data from file, or generate simple -func GetOrGenerateTemplateData(filename string) (*models.TemplateData, error) { - template, err := GetTemplateFromCache(filename) - if err != nil { - return &models.TemplateData{}, err - } - - 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 { - data.StringMap["GeneratedAt"] = template.GeneratedAt.Format(time.UnixDate) - } - } else { - app.Logger.Info(fmt.Sprintf("Loading template data for %s failed, using default template data.", filename), "err", err) - data = td.MakeBasicTemplateData(template.GeneratedAt) - } - - return &data, nil -}