Clean up param names.

This commit is contained in:
mikestefanello 2021-12-19 20:14:00 -05:00
parent 38cf009b70
commit 15974c9b77

View file

@ -35,74 +35,74 @@ func NewController(c *services.Container) Controller {
} }
// RenderPage renders a Page as an HTTP response // RenderPage renders a Page as an HTTP response
func (t *Controller) RenderPage(c echo.Context, p Page) error { func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
// Page name is required // Page name is required
if p.Name == "" { if page.Name == "" {
c.Logger().Error("page render failed due to missing name") ctx.Logger().Error("page render failed due to missing name")
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error") return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
} }
// Use the app name in configuration if a value was not set // Use the app name in configuration if a value was not set
if p.AppName == "" { if page.AppName == "" {
p.AppName = t.Container.Config.App.Name page.AppName = c.Container.Config.App.Name
} }
// Parse the templates in the page and store them in a cache, if not yet done // Parse the templates in the page and store them in a cache, if not yet done
if err := t.parsePageTemplates(p); err != nil { if err := c.parsePageTemplates(page); err != nil {
c.Logger().Errorf("failed to parse templates: %v", err) ctx.Logger().Errorf("failed to parse templates: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error") return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
} }
// Execute the parsed templates to render the page // Execute the parsed templates to render the page
buf, err := t.executeTemplates(p) buf, err := c.executeTemplates(page)
if err != nil { if err != nil {
c.Logger().Errorf("failed to execute templates: %v", err) ctx.Logger().Errorf("failed to execute templates: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error") return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
} }
// Cache this page, if caching was enabled // Cache this page, if caching was enabled
t.cachePage(c, p, buf) c.cachePage(ctx, page, buf)
// Set any headers // Set any headers
for k, v := range p.Headers { for k, v := range page.Headers {
c.Response().Header().Set(k, v) ctx.Response().Header().Set(k, v)
} }
return c.HTMLBlob(p.StatusCode, buf.Bytes()) return ctx.HTMLBlob(page.StatusCode, buf.Bytes())
} }
// cachePage caches the HTML for a given Page if the Page has caching enabled // cachePage caches the HTML for a given Page if the Page has caching enabled
func (t *Controller) cachePage(c echo.Context, p Page, html *bytes.Buffer) { func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer) {
if !p.Cache.Enabled { if !page.Cache.Enabled {
return return
} }
// If no expiration time was provided, default to the configuration value // If no expiration time was provided, default to the configuration value
if p.Cache.Expiration == 0 { if page.Cache.Expiration == 0 {
p.Cache.Expiration = t.Container.Config.Cache.Expiration.Page page.Cache.Expiration = c.Container.Config.Cache.Expiration.Page
} }
// The request URL is used as the cache key so the middleware can serve the // The request URL is used as the cache key so the middleware can serve the
// cached page on matching requests // cached page on matching requests
key := c.Request().URL.String() key := ctx.Request().URL.String()
opts := &store.Options{ opts := &store.Options{
Expiration: p.Cache.Expiration, Expiration: page.Cache.Expiration,
Tags: p.Cache.Tags, Tags: page.Cache.Tags,
} }
cp := middleware.CachedPage{ cp := middleware.CachedPage{
URL: key, URL: key,
HTML: html.Bytes(), HTML: html.Bytes(),
Headers: p.Headers, Headers: page.Headers,
StatusCode: p.StatusCode, StatusCode: page.StatusCode,
} }
err := marshaler.New(t.Container.Cache).Set(c.Request().Context(), key, cp, opts) err := marshaler.New(c.Container.Cache).Set(ctx.Request().Context(), key, cp, opts)
if err != nil { if err != nil {
c.Logger().Errorf("failed to cache page: %v", err) ctx.Logger().Errorf("failed to cache page: %v", err)
return return
} }
c.Logger().Infof("cached page") ctx.Logger().Infof("cached page")
} }
// parsePageTemplates parses the templates for the given Page and caches them to avoid duplicate operations // parsePageTemplates parses the templates for the given Page and caches them to avoid duplicate operations
@ -113,26 +113,26 @@ func (t *Controller) cachePage(c echo.Context, p Page, html *bytes.Buffer) {
// 2. The content template specified in Page.Name // 2. The content template specified in Page.Name
// 3. All templates within the components directory // 3. All templates within the components directory
// Also included is the function map provided by the funcmap package // Also included is the function map provided by the funcmap package
func (t *Controller) parsePageTemplates(p Page) error { func (c *Controller) parsePageTemplates(page Page) error {
return t.Container.Templates.Parse( return c.Container.Templates.Parse(
"controller", "controller",
p.Name, page.Name,
p.Layout, page.Layout,
[]string{ []string{
fmt.Sprintf("layouts/%s", p.Layout), fmt.Sprintf("layouts/%s", page.Layout),
fmt.Sprintf("pages/%s", p.Name), fmt.Sprintf("pages/%s", page.Name),
}, },
[]string{"components"}) []string{"components"})
} }
// executeTemplates executes the cached templates belonging to Page and renders the Page within them // executeTemplates executes the cached templates belonging to Page and renders the Page within them
func (t *Controller) executeTemplates(p Page) (*bytes.Buffer, error) { func (c *Controller) executeTemplates(page Page) (*bytes.Buffer, error) {
return t.Container.Templates.Execute("controller", p.Name, p.Layout, p) return c.Container.Templates.Execute("controller", page.Name, page.Layout, page)
} }
// Redirect redirects to a given route name with optional route parameters // Redirect redirects to a given route name with optional route parameters
func (t *Controller) Redirect(c echo.Context, route string, routeParams ...interface{}) error { func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...interface{}) error {
return c.Redirect(http.StatusFound, c.Echo().Reverse(route, routeParams)) return ctx.Redirect(http.StatusFound, ctx.Echo().Reverse(route, routeParams))
} }
// SetValidationErrorMessages sets error flash messages for validation failures of a given struct // SetValidationErrorMessages sets error flash messages for validation failures of a given struct
@ -143,7 +143,7 @@ func (t *Controller) Redirect(c echo.Context, route string, routeParams ...inter
// of the field used in the error messages, for example: // of the field used in the error messages, for example:
// - FirstName string `form:"first-name" validate:"required" label:"First name"` // - FirstName string `form:"first-name" validate:"required" label:"First name"`
// Only a few validator tags are supported below. Expand them as needed. // Only a few validator tags are supported below. Expand them as needed.
func (t *Controller) SetValidationErrorMessages(c echo.Context, err error, data interface{}) { func (c *Controller) SetValidationErrorMessages(ctx echo.Context, err error, data interface{}) {
ves, ok := err.(validator.ValidationErrors) ves, ok := err.(validator.ValidationErrors)
if !ok { if !ok {
return return
@ -175,6 +175,6 @@ func (t *Controller) SetValidationErrorMessages(c echo.Context, err error, data
message = "%s is not a valid value." message = "%s is not a valid value."
} }
msg.Danger(c, fmt.Sprintf(message, "<strong>"+label+"</strong>")) msg.Danger(ctx, fmt.Sprintf(message, "<strong>"+label+"</strong>"))
} }
} }