Moved app consts to config package.
This commit is contained in:
parent
959eeda35f
commit
590910bc41
3 changed files with 11 additions and 19 deletions
|
|
@ -22,11 +22,6 @@ import (
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
TemplateDir = "views"
|
|
||||||
TemplateExt = ".gohtml"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Cache of compiled page templates
|
// Cache of compiled page templates
|
||||||
templates = sync.Map{}
|
templates = sync.Map{}
|
||||||
|
|
@ -111,18 +106,18 @@ func (t *Controller) parsePageTemplates(p Page) error {
|
||||||
// changes without having the restart the server
|
// changes without having the restart the server
|
||||||
if _, ok := templates.Load(p.Name); !ok || t.Container.Config.App.Environment == config.EnvLocal {
|
if _, ok := templates.Load(p.Name); !ok || t.Container.Config.App.Environment == config.EnvLocal {
|
||||||
parsed, err :=
|
parsed, err :=
|
||||||
template.New(p.Layout+TemplateExt).
|
template.New(p.Layout+config.TemplateExt).
|
||||||
Funcs(funcMap).
|
Funcs(funcMap).
|
||||||
ParseFiles(
|
ParseFiles(
|
||||||
fmt.Sprintf("%s/layouts/%s%s", templatePath, p.Layout, TemplateExt),
|
fmt.Sprintf("%s/layouts/%s%s", templatePath, p.Layout, config.TemplateExt),
|
||||||
fmt.Sprintf("%s/pages/%s%s", templatePath, p.Name, TemplateExt),
|
fmt.Sprintf("%s/pages/%s%s", templatePath, p.Name, config.TemplateExt),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -143,7 +138,7 @@ func (t *Controller) executeTemplates(c echo.Context, p Page) (*bytes.Buffer, er
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -161,5 +156,5 @@ func (t *Controller) Redirect(c echo.Context, route string, routeParams ...inter
|
||||||
func getTemplatesDirectoryPath() string {
|
func getTemplatesDirectoryPath() string {
|
||||||
_, b, _, _ := runtime.Caller(0)
|
_, b, _, _ := runtime.Caller(0)
|
||||||
d := path.Join(path.Dir(b))
|
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 (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"goweb/config"
|
||||||
"goweb/middleware"
|
"goweb/middleware"
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
|
|
@ -14,17 +15,12 @@ import (
|
||||||
"goweb/container"
|
"goweb/container"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
StaticDir = "static"
|
|
||||||
StaticPrefix = "files"
|
|
||||||
)
|
|
||||||
|
|
||||||
func BuildRouter(c *container.Container) {
|
func BuildRouter(c *container.Container) {
|
||||||
// Static files with proper cache control
|
// 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
|
// funcmap.File() should be used in templates to append a cache key to the URL in order to break cache
|
||||||
// after each server restart
|
// after each server restart
|
||||||
c.Web.Group("", middleware.CacheControl(c.Config.Cache.MaxAge.StaticFile)).
|
c.Web.Group("", middleware.CacheControl(c.Config.Cache.MaxAge.StaticFile)).
|
||||||
Static(StaticPrefix, StaticDir)
|
Static(config.StaticPrefix, config.StaticDir)
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
g := c.Web.Group("",
|
g := c.Web.Group("",
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"goweb/config"
|
||||||
|
|
||||||
"github.com/Masterminds/sprig"
|
"github.com/Masterminds/sprig"
|
||||||
"github.com/labstack/gommon/random"
|
"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
|
// File appends a cache key to a given filepath so it can remain cached until the app is restarted
|
||||||
func File(filepath string) string {
|
func File(filepath string) string {
|
||||||
// TODO: Use const for path prefix
|
return fmt.Sprintf("/%s/%s?v=%s", config.StaticPrefix, filepath, CacheKey)
|
||||||
return fmt.Sprintf("/files/%s?v=%s", filepath, CacheKey)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Link(url, text, currentPath string, classes ...string) template.HTML {
|
func Link(url, text, currentPath string, classes ...string) template.HTML {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue