Migrate from templates to Gomponents (#103)
This commit is contained in:
parent
0bf9ab7189
commit
051d032038
104 changed files with 2768 additions and 2824 deletions
|
|
@ -1,66 +1,13 @@
|
|||
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"
|
||||
)
|
||||
|
||||
// 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())
|
||||
|
||||
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")
|
||||
|
||||
return ctx.HTMLBlob(page.StatusCode, page.HTML)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CacheControl sets a Cache-Control header with a given max age
|
||||
// CacheControl sets a Cache-Control header with a given max age.
|
||||
func CacheControl(maxAge time.Duration) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(ctx echo.Context) error {
|
||||
|
|
|
|||
|
|
@ -1,52 +1,14 @@
|
|||
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)
|
||||
}
|
||||
|
||||
func TestCacheControl(t *testing.T) {
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
_ = tests.ExecuteMiddleware(ctx, CacheControl(time.Second*5))
|
||||
|
|
|
|||
17
pkg/middleware/config.go
Normal file
17
pkg/middleware/config.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
)
|
||||
|
||||
// Config stores the configuration in the request so it can be accessed by the ui.
|
||||
func Config(cfg *config.Config) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(ctx echo.Context) error {
|
||||
ctx.Set(context.ConfigKey, cfg)
|
||||
return next(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
22
pkg/middleware/config_test.go
Normal file
22
pkg/middleware/config_test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
cfg := &config.Config{}
|
||||
err := tests.ExecuteMiddleware(ctx, Config(cfg))
|
||||
require.NoError(t, err)
|
||||
|
||||
got, ok := ctx.Get(context.ConfigKey).(*config.Config)
|
||||
require.True(t, ok)
|
||||
assert.Same(t, got, cfg)
|
||||
}
|
||||
|
|
@ -12,7 +12,7 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// LoadUser loads the user based on the ID provided as a path parameter
|
||||
// LoadUser loads the user based on the ID provided as a path parameter.
|
||||
func LoadUser(orm *ent.Client) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue