2021-12-03 03:11:01 -08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2021-12-05 17:22:45 -08:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2021-12-03 03:11:01 -08:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"goweb/config"
|
|
|
|
"goweb/container"
|
|
|
|
"goweb/funcmap"
|
2021-12-07 18:36:57 -08:00
|
|
|
"goweb/middleware"
|
|
|
|
|
|
|
|
"github.com/eko/gocache/v2/marshaler"
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-06 11:53:28 -08:00
|
|
|
"github.com/eko/gocache/v2/store"
|
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Cache of compiled page templates
|
|
|
|
templates = sync.Map{}
|
|
|
|
|
|
|
|
// Template function map
|
|
|
|
funcMap = funcmap.GetFuncMap()
|
2021-12-05 17:22:45 -08:00
|
|
|
|
|
|
|
templatePath = getTemplatesDirectoryPath()
|
2021-12-03 03:11:01 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Controller struct {
|
|
|
|
Container *container.Container
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewController(c *container.Container) Controller {
|
|
|
|
return Controller{
|
|
|
|
Container: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 18:51:21 -08:00
|
|
|
// TODO: Audit error handling (ie NewHTTPError)
|
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
func (t *Controller) RenderPage(c echo.Context, p Page) error {
|
|
|
|
if p.Name == "" {
|
2021-12-07 18:36:57 -08:00
|
|
|
c.Logger().Error("page render failed due to missing name")
|
2021-12-03 03:11:01 -08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.AppName == "" {
|
|
|
|
p.AppName = t.Container.Config.App.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.parsePageTemplates(p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:53:28 -08:00
|
|
|
buf, err := t.executeTemplates(c, p)
|
2021-12-03 03:11:01 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-07 18:36:57 -08:00
|
|
|
t.cachePage(c, p, buf)
|
|
|
|
|
|
|
|
// Set any headers
|
|
|
|
for k, v := range p.Headers {
|
|
|
|
c.Response().Header().Set(k, v)
|
|
|
|
}
|
2021-12-06 11:53:28 -08:00
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
return c.HTMLBlob(p.StatusCode, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
2021-12-07 18:36:57 -08:00
|
|
|
func (t *Controller) cachePage(c echo.Context, p Page, html *bytes.Buffer) {
|
2021-12-06 11:53:28 -08:00
|
|
|
if !p.Cache.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-11 16:32:34 -08:00
|
|
|
if p.Cache.Expiration == 0 {
|
|
|
|
p.Cache.Expiration = t.Container.Config.Cache.Expiration.Page
|
2021-12-06 11:53:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
key := c.Request().URL.String()
|
|
|
|
opts := &store.Options{
|
2021-12-11 16:32:34 -08:00
|
|
|
Expiration: p.Cache.Expiration,
|
2021-12-06 11:53:28 -08:00
|
|
|
Tags: p.Cache.Tags,
|
|
|
|
}
|
2021-12-07 18:36:57 -08:00
|
|
|
cp := middleware.CachedPage{
|
2021-12-11 16:32:34 -08:00
|
|
|
URL: key,
|
2021-12-07 18:36:57 -08:00
|
|
|
HTML: html.Bytes(),
|
|
|
|
Headers: p.Headers,
|
|
|
|
StatusCode: p.StatusCode,
|
|
|
|
}
|
|
|
|
err := marshaler.New(t.Container.Cache).Set(c.Request().Context(), key, cp, opts)
|
2021-12-06 11:53:28 -08:00
|
|
|
if err != nil {
|
2021-12-08 18:55:30 -08:00
|
|
|
c.Logger().Errorf("failed to cache page: %v", err)
|
2021-12-06 11:53:28 -08:00
|
|
|
return
|
|
|
|
}
|
2021-12-07 18:36:57 -08:00
|
|
|
|
2021-12-08 18:55:30 -08:00
|
|
|
c.Logger().Infof("cached page")
|
2021-12-06 11:53:28 -08:00
|
|
|
}
|
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
func (t *Controller) parsePageTemplates(p Page) error {
|
|
|
|
// Check if the template has not yet been parsed or if the app environment is local, so that templates reflect
|
|
|
|
// changes without having the restart the server
|
|
|
|
if _, ok := templates.Load(p.Name); !ok || t.Container.Config.App.Environment == config.EnvLocal {
|
|
|
|
parsed, err :=
|
2021-12-10 05:33:49 -08:00
|
|
|
template.New(p.Layout+config.TemplateExt).
|
2021-12-03 03:11:01 -08:00
|
|
|
Funcs(funcMap).
|
|
|
|
ParseFiles(
|
2021-12-10 05:33:49 -08:00
|
|
|
fmt.Sprintf("%s/layouts/%s%s", templatePath, p.Layout, config.TemplateExt),
|
|
|
|
fmt.Sprintf("%s/pages/%s%s", templatePath, p.Name, config.TemplateExt),
|
2021-12-03 03:11:01 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-10 05:33:49 -08:00
|
|
|
parsed, err = parsed.ParseGlob(fmt.Sprintf("%s/components/*%s", templatePath, config.TemplateExt))
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the template so this process only happens once
|
|
|
|
templates.Store(p.Name, parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:53:28 -08:00
|
|
|
func (t *Controller) executeTemplates(c echo.Context, p Page) (*bytes.Buffer, error) {
|
|
|
|
tmpl, ok := templates.Load(p.Name)
|
|
|
|
if !ok {
|
2021-12-07 18:36:57 -08:00
|
|
|
c.Logger().Error("uncached page template requested")
|
2021-12-06 11:53:28 -08:00
|
|
|
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2021-12-10 05:33:49 -08:00
|
|
|
err := tmpl.(*template.Template).ExecuteTemplate(buf, p.Layout+config.TemplateExt, p)
|
2021-12-06 11:53:28 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
func (t *Controller) Redirect(c echo.Context, route string, routeParams ...interface{}) error {
|
|
|
|
return c.Redirect(http.StatusFound, c.Echo().Reverse(route, routeParams))
|
|
|
|
}
|
2021-12-05 17:22:45 -08:00
|
|
|
|
|
|
|
// getTemplatesDirectoryPath gets the templates directory path
|
|
|
|
// This is needed incase this is called from a package outside of main,
|
|
|
|
// such as testing
|
|
|
|
func getTemplatesDirectoryPath() string {
|
|
|
|
_, b, _, _ := runtime.Caller(0)
|
|
|
|
d := path.Join(path.Dir(b))
|
2021-12-10 05:33:49 -08:00
|
|
|
return filepath.Join(filepath.Dir(d), config.TemplateDir)
|
2021-12-05 17:22:45 -08:00
|
|
|
}
|