2021-12-12 19:09:13 -08:00
|
|
|
package controller
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2021-12-07 18:36:57 -08:00
|
|
|
"goweb/middleware"
|
2021-12-18 07:07:12 -08:00
|
|
|
"goweb/services"
|
2021-12-14 17:54:47 -08:00
|
|
|
|
2021-12-07 18:36:57 -08:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// Controller provides base functionality and dependencies to routes.
|
|
|
|
// The proposed pattern is to embed a Controller in each individual route struct and to use
|
|
|
|
// the router to inject the container so your routes have access to the services within the container
|
2021-12-03 03:11:01 -08:00
|
|
|
type Controller struct {
|
2021-12-19 07:03:42 -08:00
|
|
|
// Container stores a services container which contains dependencies
|
2021-12-18 07:07:12 -08:00
|
|
|
Container *services.Container
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// NewController creates a new Controller
|
2021-12-18 07:07:12 -08:00
|
|
|
func NewController(c *services.Container) Controller {
|
2021-12-03 03:11:01 -08:00
|
|
|
return Controller{
|
|
|
|
Container: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// RenderPage renders a Page as an HTTP response
|
2021-12-19 17:14:00 -08:00
|
|
|
func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
|
2021-12-23 14:09:44 -08:00
|
|
|
var buf *bytes.Buffer
|
|
|
|
var err error
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// Page name is required
|
2021-12-19 17:14:00 -08:00
|
|
|
if page.Name == "" {
|
|
|
|
ctx.Logger().Error("page render failed due to missing name")
|
2021-12-23 14:09:44 -08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// Use the app name in configuration if a value was not set
|
2021-12-19 17:14:00 -08:00
|
|
|
if page.AppName == "" {
|
|
|
|
page.AppName = c.Container.Config.App.Name
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2021-12-23 14:09:44 -08:00
|
|
|
// Check if this is an HTMX request
|
|
|
|
if page.HTMX.Request.Enabled && !page.HTMX.Request.Boosted {
|
|
|
|
// Disable caching
|
|
|
|
page.Cache.Enabled = false
|
|
|
|
|
|
|
|
// Parse and execute
|
|
|
|
buf, err = c.Container.TemplateRenderer.ParseAndExecute(
|
|
|
|
"page:htmx",
|
|
|
|
page.Name,
|
|
|
|
"htmx",
|
|
|
|
[]string{
|
|
|
|
"htmx",
|
|
|
|
fmt.Sprintf("pages/%s", page.Name),
|
|
|
|
},
|
|
|
|
[]string{"components"},
|
|
|
|
page,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Parse and execute the templates for the Page
|
|
|
|
// As mentioned in the documentation for the Page struct, the templates used for the page will be:
|
|
|
|
// 1. The layout/base template specified in Page.Layout
|
|
|
|
// 2. The content template specified in Page.Name
|
|
|
|
// 3. All templates within the components directory
|
|
|
|
// Also included is the function map provided by the funcmap package
|
|
|
|
buf, err = c.Container.TemplateRenderer.ParseAndExecute(
|
|
|
|
"page",
|
|
|
|
page.Name,
|
|
|
|
page.Layout,
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("layouts/%s", page.Layout),
|
|
|
|
fmt.Sprintf("pages/%s", page.Name),
|
|
|
|
},
|
|
|
|
[]string{"components"},
|
|
|
|
page,
|
|
|
|
)
|
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
if err != nil {
|
2021-12-21 08:00:32 -08:00
|
|
|
ctx.Logger().Errorf("failed to parse and execute templates: %v", err)
|
2021-12-23 14:09:44 -08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2021-12-23 14:09:44 -08:00
|
|
|
// Set the status code
|
|
|
|
ctx.Response().Status = page.StatusCode
|
2021-12-07 18:36:57 -08:00
|
|
|
|
|
|
|
// Set any headers
|
2021-12-19 17:14:00 -08:00
|
|
|
for k, v := range page.Headers {
|
|
|
|
ctx.Response().Header().Set(k, v)
|
2021-12-07 18:36:57 -08:00
|
|
|
}
|
2021-12-06 11:53:28 -08:00
|
|
|
|
2021-12-23 14:09:44 -08:00
|
|
|
// Apply the HTMX response, if one
|
|
|
|
if page.HTMX.Response != nil {
|
|
|
|
page.HTMX.Response.Apply(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache this page, if caching was enabled
|
|
|
|
c.cachePage(ctx, page, buf)
|
|
|
|
|
|
|
|
return ctx.HTMLBlob(ctx.Response().Status, buf.Bytes())
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// cachePage caches the HTML for a given Page if the Page has caching enabled
|
2021-12-19 17:14:00 -08:00
|
|
|
func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer) {
|
|
|
|
if !page.Cache.Enabled {
|
2021-12-06 11:53:28 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// If no expiration time was provided, default to the configuration value
|
2021-12-19 17:14:00 -08:00
|
|
|
if page.Cache.Expiration == 0 {
|
|
|
|
page.Cache.Expiration = c.Container.Config.Cache.Expiration.Page
|
2021-12-06 11:53:28 -08:00
|
|
|
}
|
|
|
|
|
2021-12-23 14:09:44 -08:00
|
|
|
// Extract the headers
|
|
|
|
headers := make(map[string]string)
|
|
|
|
for k, v := range ctx.Response().Header() {
|
|
|
|
headers[k] = v[0]
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// The request URL is used as the cache key so the middleware can serve the
|
|
|
|
// cached page on matching requests
|
2021-12-19 17:14:00 -08:00
|
|
|
key := ctx.Request().URL.String()
|
2021-12-06 11:53:28 -08:00
|
|
|
opts := &store.Options{
|
2021-12-19 17:14:00 -08:00
|
|
|
Expiration: page.Cache.Expiration,
|
|
|
|
Tags: page.Cache.Tags,
|
2021-12-06 11:53:28 -08:00
|
|
|
}
|
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(),
|
2021-12-23 14:09:44 -08:00
|
|
|
Headers: headers,
|
|
|
|
StatusCode: ctx.Response().Status,
|
2021-12-07 18:36:57 -08:00
|
|
|
}
|
2021-12-19 07:03:42 -08:00
|
|
|
|
2021-12-19 17:14:00 -08:00
|
|
|
err := marshaler.New(c.Container.Cache).Set(ctx.Request().Context(), key, cp, opts)
|
2021-12-06 11:53:28 -08:00
|
|
|
if err != nil {
|
2021-12-19 17:14:00 -08:00
|
|
|
ctx.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-19 17:14:00 -08:00
|
|
|
ctx.Logger().Infof("cached page")
|
2021-12-06 11:53:28 -08:00
|
|
|
}
|
|
|
|
|
2021-12-19 07:03:42 -08:00
|
|
|
// Redirect redirects to a given route name with optional route parameters
|
2021-12-19 17:14:00 -08:00
|
|
|
func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...interface{}) error {
|
2021-12-23 20:04:00 -08:00
|
|
|
url := ctx.Echo().Reverse(route, routeParams)
|
2021-12-24 05:42:42 -08:00
|
|
|
// TODO: HTMX redirect?
|
2021-12-23 20:04:00 -08:00
|
|
|
return ctx.Redirect(http.StatusFound, url)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
2021-12-05 17:22:45 -08:00
|
|
|
|
2021-12-23 17:58:49 -08:00
|
|
|
func (c *Controller) Fail(ctx echo.Context, err error, log string) error {
|
|
|
|
ctx.Logger().Errorf("%s: %v", log, err)
|
2021-12-24 05:42:42 -08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
2021-12-14 17:54:47 -08:00
|
|
|
}
|