Moved app consts to config package.
This commit is contained in:
parent
959eeda35f
commit
590910bc41
@ -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)
|
||||
}
|
||||
|
@ -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("",
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user