2021-12-03 05:18:40 -08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-03-10 09:46:53 -07:00
|
|
|
"errors"
|
2021-12-03 05:18:40 -08:00
|
|
|
"fmt"
|
2021-12-19 10:01:52 -08:00
|
|
|
"net/http"
|
2021-12-06 11:53:28 -08:00
|
|
|
"time"
|
2021-12-03 05:18:40 -08:00
|
|
|
|
2022-11-02 16:23:26 -07:00
|
|
|
"github.com/mikestefanello/pagoda/pkg/context"
|
|
|
|
"github.com/mikestefanello/pagoda/pkg/services"
|
2021-12-19 10:01:52 -08:00
|
|
|
|
2024-03-14 17:43:27 -07:00
|
|
|
libstore "github.com/eko/gocache/lib/v4/store"
|
2021-12-03 05:18:40 -08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2022-01-13 18:13:41 -08:00
|
|
|
// CachedPageGroup stores the cache group for cached pages
|
|
|
|
const CachedPageGroup = "page"
|
|
|
|
|
2021-12-21 17:49:05 -08:00
|
|
|
// CachedPage is what is used to store a rendered Page in the cache
|
2021-12-07 18:36:57 -08:00
|
|
|
type CachedPage struct {
|
2021-12-21 17:49:05 -08:00
|
|
|
// URL stores the URL of the requested page
|
|
|
|
URL string
|
|
|
|
|
|
|
|
// HTML stores the complete HTML of the rendered Page
|
|
|
|
HTML []byte
|
|
|
|
|
|
|
|
// StatusCode stores the HTTP status code
|
2021-12-07 18:36:57 -08:00
|
|
|
StatusCode int
|
2021-12-21 17:49:05 -08:00
|
|
|
|
|
|
|
// Headers stores the HTTP headers
|
|
|
|
Headers map[string]string
|
2021-12-07 18:36:57 -08:00
|
|
|
}
|
|
|
|
|
2021-12-21 17:49:05 -08:00
|
|
|
// 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.
|
2022-01-13 18:13:41 -08:00
|
|
|
func ServeCachedPage(ch *services.CacheClient) echo.MiddlewareFunc {
|
2021-12-07 18:36:57 -08:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2021-12-19 10:01:52 -08:00
|
|
|
// Skip non GET requests
|
|
|
|
if c.Request().Method != http.MethodGet {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if the user is authenticated
|
|
|
|
if c.Get(context.AuthenticatedUserKey) != nil {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2021-12-21 17:49:05 -08:00
|
|
|
// Attempt to load from cache
|
2022-01-13 18:13:41 -08:00
|
|
|
res, err := ch.
|
|
|
|
Get().
|
|
|
|
Group(CachedPageGroup).
|
|
|
|
Key(c.Request().URL.String()).
|
|
|
|
Type(new(CachedPage)).
|
|
|
|
Fetch(c.Request().Context())
|
|
|
|
|
2021-12-07 18:36:57 -08:00
|
|
|
if err != nil {
|
2022-01-08 21:23:26 -08:00
|
|
|
switch {
|
2024-03-14 17:43:27 -07:00
|
|
|
case errors.Is(err, &libstore.NotFound{}):
|
2021-12-21 17:49:05 -08:00
|
|
|
c.Logger().Info("no cached page found")
|
2022-01-08 21:23:26 -08:00
|
|
|
case context.IsCanceledError(err):
|
|
|
|
return nil
|
|
|
|
default:
|
2021-12-08 18:55:30 -08:00
|
|
|
c.Logger().Errorf("failed getting cached page: %v", err)
|
2021-12-07 18:36:57 -08:00
|
|
|
}
|
2022-01-08 21:23:26 -08:00
|
|
|
|
2021-12-07 18:36:57 -08:00
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2021-12-08 15:58:55 -08:00
|
|
|
page, ok := res.(*CachedPage)
|
|
|
|
if !ok {
|
2021-12-08 18:55:30 -08:00
|
|
|
c.Logger().Errorf("failed casting cached page")
|
2021-12-08 15:58:55 -08:00
|
|
|
return next(c)
|
|
|
|
}
|
2021-12-07 18:36:57 -08:00
|
|
|
|
2021-12-21 17:49:05 -08:00
|
|
|
// Set any headers
|
2021-12-07 18:36:57 -08:00
|
|
|
if page.Headers != nil {
|
|
|
|
for k, v := range page.Headers {
|
|
|
|
c.Response().Header().Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 17:49:05 -08:00
|
|
|
|
2021-12-19 11:56:00 -08:00
|
|
|
c.Logger().Info("serving cached page")
|
2021-12-07 18:36:57 -08:00
|
|
|
|
|
|
|
return c.HTMLBlob(page.StatusCode, page.HTML)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 17:49:05 -08:00
|
|
|
// CacheControl sets a Cache-Control header with a given max age
|
2021-12-06 11:53:28 -08:00
|
|
|
func CacheControl(maxAge time.Duration) echo.MiddlewareFunc {
|
2021-12-03 05:18:40 -08:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
v := "no-cache, no-store"
|
|
|
|
if maxAge > 0 {
|
2021-12-06 11:53:28 -08:00
|
|
|
v = fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds())
|
2021-12-03 05:18:40 -08:00
|
|
|
}
|
|
|
|
c.Response().Header().Set("Cache-Control", v)
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|