From 590910bc4156a3477c75db4f76b3901dd2b2b3c5 Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Fri, 10 Dec 2021 08:33:49 -0500 Subject: [PATCH] Moved app consts to config package. --- controllers/controller.go | 17 ++++++----------- controllers/router.go | 8 ++------ funcmap/funcmap.go | 5 +++-- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/controllers/controller.go b/controllers/controller.go index 6c1ffcd..30867c3 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -22,11 +22,6 @@ import ( "github.com/labstack/echo/v4" ) -const ( - TemplateDir = "views" - TemplateExt = ".gohtml" -) - var ( // Cache of compiled page templates templates = sync.Map{} @@ -111,18 +106,18 @@ func (t *Controller) parsePageTemplates(p Page) error { // changes without having the restart the server if _, ok := templates.Load(p.Name); !ok || t.Container.Config.App.Environment == config.EnvLocal { parsed, err := - template.New(p.Layout+TemplateExt). + template.New(p.Layout+config.TemplateExt). Funcs(funcMap). ParseFiles( - fmt.Sprintf("%s/layouts/%s%s", templatePath, p.Layout, TemplateExt), - fmt.Sprintf("%s/pages/%s%s", templatePath, p.Name, TemplateExt), + fmt.Sprintf("%s/layouts/%s%s", templatePath, p.Layout, config.TemplateExt), + fmt.Sprintf("%s/pages/%s%s", templatePath, p.Name, config.TemplateExt), ) if err != nil { return err } - parsed, err = parsed.ParseGlob(fmt.Sprintf("%s/components/*%s", templatePath, TemplateExt)) + parsed, err = parsed.ParseGlob(fmt.Sprintf("%s/components/*%s", templatePath, config.TemplateExt)) if err != nil { return err @@ -143,7 +138,7 @@ func (t *Controller) executeTemplates(c echo.Context, p Page) (*bytes.Buffer, er } buf := new(bytes.Buffer) - err := tmpl.(*template.Template).ExecuteTemplate(buf, p.Layout+TemplateExt, p) + err := tmpl.(*template.Template).ExecuteTemplate(buf, p.Layout+config.TemplateExt, p) if err != nil { return nil, err } @@ -161,5 +156,5 @@ func (t *Controller) Redirect(c echo.Context, route string, routeParams ...inter func getTemplatesDirectoryPath() string { _, b, _, _ := runtime.Caller(0) d := path.Join(path.Dir(b)) - return filepath.Join(filepath.Dir(d), TemplateDir) + return filepath.Join(filepath.Dir(d), config.TemplateDir) } diff --git a/controllers/router.go b/controllers/router.go index 02bc361..f79cc21 100644 --- a/controllers/router.go +++ b/controllers/router.go @@ -3,6 +3,7 @@ package controllers import ( "net/http" + "goweb/config" "goweb/middleware" "github.com/gorilla/sessions" @@ -14,17 +15,12 @@ import ( "goweb/container" ) -const ( - StaticDir = "static" - StaticPrefix = "files" -) - func BuildRouter(c *container.Container) { // Static files with proper cache control // funcmap.File() should be used in templates to append a cache key to the URL in order to break cache // after each server restart c.Web.Group("", middleware.CacheControl(c.Config.Cache.MaxAge.StaticFile)). - Static(StaticPrefix, StaticDir) + Static(config.StaticPrefix, config.StaticDir) // Middleware g := c.Web.Group("", diff --git a/funcmap/funcmap.go b/funcmap/funcmap.go index 3457a53..f032553 100644 --- a/funcmap/funcmap.go +++ b/funcmap/funcmap.go @@ -6,6 +6,8 @@ import ( "reflect" "strings" + "goweb/config" + "github.com/Masterminds/sprig" "github.com/labstack/gommon/random" ) @@ -45,8 +47,7 @@ func HasField(v interface{}, name string) bool { // File appends a cache key to a given filepath so it can remain cached until the app is restarted func File(filepath string) string { - // TODO: Use const for path prefix - return fmt.Sprintf("/files/%s?v=%s", filepath, CacheKey) + return fmt.Sprintf("/%s/%s?v=%s", config.StaticPrefix, filepath, CacheKey) } func Link(url, text, currentPath string, classes ...string) template.HTML {