Skip to content

Commit

Permalink
Removed templates, template renderer, funcmap, and page.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikestefanello committed Feb 23, 2025
1 parent 055560f commit 149005c
Show file tree
Hide file tree
Showing 36 changed files with 71 additions and 1,766 deletions.
56 changes: 0 additions & 56 deletions pkg/funcmap/funcmap.go

This file was deleted.

52 changes: 0 additions & 52 deletions pkg/funcmap/funcmap_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/handlers/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/page"
"github.com/mikestefanello/pagoda/pkg/pager"
"github.com/mikestefanello/pagoda/pkg/routenames"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/pkg/ui"
Expand All @@ -26,7 +26,7 @@ func (h *Pages) Routes(g *echo.Group) {
}

func (h *Pages) Home(ctx echo.Context) error {
pgr := page.NewPager(ctx, 4)
pgr := pager.NewPager(ctx, 4)

return ui.Home(ctx, ui.Posts{
Posts: h.fetchPosts(&pgr),
Expand All @@ -35,7 +35,7 @@ func (h *Pages) Home(ctx echo.Context) error {
}

// fetchPosts is a mock example of fetching posts to illustrate how paging works.
func (h *Pages) fetchPosts(pager *page.Pager) []ui.Post {
func (h *Pages) fetchPosts(pager *pager.Pager) []ui.Post {
pager.SetItems(20)
posts := make([]ui.Post, 20)

Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func BuildRouter(c *services.Container) error {
}),
middleware.Session(cookieStore),
middleware.LoadAuthenticatedUser(c.Auth),
middleware.ServeCachedPage(c.TemplateRenderer),
middleware.ServeCachedPage,
echomw.CSRFWithConfig(echomw.CSRFConfig{
TokenLookup: "form:csrf",
CookieHTTPOnly: true,
Expand Down
56 changes: 13 additions & 43 deletions pkg/middleware/cache.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,32 @@
package middleware

import (
"errors"
"fmt"
"net/http"
"time"

"github.com/mikestefanello/pagoda/pkg/context"
"github.com/mikestefanello/pagoda/pkg/log"
"github.com/mikestefanello/pagoda/pkg/services"

"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/context"
)

// ServeCachedPage attempts to load a page from the cache by matching on the complete request URL
// If a page is cached for the requested URL, it will be served here and the request terminated.
// Any request made by an authenticated user or that is not a GET will be skipped.
func ServeCachedPage(t *services.TemplateRenderer) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
// Skip non GET requests
if ctx.Request().Method != http.MethodGet {
return next(ctx)
}

// Skip if the user is authenticated
if ctx.Get(context.AuthenticatedUserKey) != nil {
return next(ctx)
}

// Attempt to load from cache
page, err := t.GetCachedPage(ctx, ctx.Request().URL.String())
func ServeCachedPage(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {

if err != nil {
switch {
case errors.Is(err, services.ErrCacheMiss):
case context.IsCanceledError(err):
return nil
default:
log.Ctx(ctx).Error("failed getting cached page",
"error", err,
)
}

return next(ctx)
}

// Set any headers
if page.Headers != nil {
for k, v := range page.Headers {
ctx.Response().Header().Set(k, v)
}
}

log.Ctx(ctx).Debug("serving cached page")
// Skip non GET requests
if ctx.Request().Method != http.MethodGet {
return next(ctx)
}

return ctx.HTMLBlob(page.StatusCode, page.HTML)
// Skip if the user is authenticated
if ctx.Get(context.AuthenticatedUserKey) != nil {
return next(ctx)
}

// TODO keep this functionality?
return next(ctx)
}
}

Expand Down
70 changes: 33 additions & 37 deletions pkg/middleware/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
package middleware

import (
"net/http"
"testing"
"time"

"github.com/mikestefanello/pagoda/pkg/page"
"github.com/mikestefanello/pagoda/pkg/tests"
"github.com/mikestefanello/pagoda/templates"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

func TestServeCachedPage(t *testing.T) {
// Cache a page
ctx, rec := tests.NewContext(c.Web, "/cache")
p := page.New(ctx)
p.Layout = templates.LayoutHTMX
p.Name = templates.PageHome
p.Cache.Enabled = true
p.Cache.Expiration = time.Minute
p.StatusCode = http.StatusCreated
p.Headers["a"] = "b"
p.Headers["c"] = "d"
err := c.TemplateRenderer.RenderPage(ctx, p)
output := rec.Body.Bytes()
require.NoError(t, err)

// Request the URL of the cached page
ctx, rec = tests.NewContext(c.Web, "/cache")
err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.TemplateRenderer))
assert.NoError(t, err)
assert.Equal(t, p.StatusCode, ctx.Response().Status)
assert.Equal(t, p.Headers["a"], ctx.Response().Header().Get("a"))
assert.Equal(t, p.Headers["c"], ctx.Response().Header().Get("c"))
assert.Equal(t, output, rec.Body.Bytes())

// Login and try again
tests.InitSession(ctx)
err = c.Auth.Login(ctx, usr.ID)
require.NoError(t, err)
_ = tests.ExecuteMiddleware(ctx, LoadAuthenticatedUser(c.Auth))
err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.TemplateRenderer))
assert.Nil(t, err)
}
// TODO keep this?
//func TestServeCachedPage(t *testing.T) {
// // Cache a page
// ctx, rec := tests.NewContext(c.Web, "/cache")
// p := page.New(ctx)
// p.Layout = templates.LayoutHTMX
// p.Name = templates.PageHome
// p.Cache.Enabled = true
// p.Cache.Expiration = time.Minute
// p.StatusCode = http.StatusCreated
// p.Headers["a"] = "b"
// p.Headers["c"] = "d"
// err := c.TemplateRenderer.RenderPage(ctx, p)
// output := rec.Body.Bytes()
// require.NoError(t, err)
//
// // Request the URL of the cached page
// ctx, rec = tests.NewContext(c.Web, "/cache")
// err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.TemplateRenderer))
// assert.NoError(t, err)
// assert.Equal(t, p.StatusCode, ctx.Response().Status)
// assert.Equal(t, p.Headers["a"], ctx.Response().Header().Get("a"))
// assert.Equal(t, p.Headers["c"], ctx.Response().Header().Get("c"))
// assert.Equal(t, output, rec.Body.Bytes())
//
// // Login and try again
// tests.InitSession(ctx)
// err = c.Auth.Login(ctx, usr.ID)
// require.NoError(t, err)
// _ = tests.ExecuteMiddleware(ctx, LoadAuthenticatedUser(c.Auth))
// err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.TemplateRenderer))
// assert.Nil(t, err)
//}

func TestCacheControl(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/")
Expand Down
Loading

0 comments on commit 149005c

Please sign in to comment.