0.29.1原版
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"encoding/xml"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/labstack/echo/v5/middleware"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/usememos/memos/internal/profile"
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
//go:embed dist/*
|
||||
var embeddedFiles embed.FS
|
||||
|
||||
const (
|
||||
frontendHTMLCacheControl = "no-cache, no-store, must-revalidate"
|
||||
frontendStaticAssetCacheControl = "public, max-age=3600"
|
||||
frontendHashedAssetCacheControl = "public, max-age=3600, immutable"
|
||||
)
|
||||
|
||||
type FrontendService struct {
|
||||
Profile *profile.Profile
|
||||
Store *store.Store
|
||||
}
|
||||
|
||||
func NewFrontendService(profile *profile.Profile, store *store.Store) *FrontendService {
|
||||
return &FrontendService{
|
||||
Profile: profile,
|
||||
Store: store,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *FrontendService) Serve(_ context.Context, e *echo.Echo) {
|
||||
frontendFS := getFileSystem("dist")
|
||||
skipper := func(c *echo.Context) bool {
|
||||
requestPath := c.Request().URL.Path
|
||||
if shouldSkipFrontendStatic(requestPath) {
|
||||
return true
|
||||
}
|
||||
|
||||
setFrontendCacheHeaders(c, requestPath)
|
||||
return false
|
||||
}
|
||||
|
||||
// Route to serve the frontend static assets.
|
||||
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||||
Filesystem: frontendFS,
|
||||
Skipper: skipper,
|
||||
}))
|
||||
|
||||
e.Use(spaFallbackMiddleware(frontendFS))
|
||||
s.registerRoutes(e)
|
||||
}
|
||||
|
||||
func spaFallbackMiddleware(frontendFS fs.FS) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) error {
|
||||
err := next(c)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
requestPath := c.Request().URL.Path
|
||||
if shouldSkipFrontendStatic(requestPath) || !shouldServeFrontendHTML(requestPath) || echo.StatusCode(err) != http.StatusNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
setFrontendCacheHeaders(c, requestPath)
|
||||
return c.FileFS("index.html", frontendFS)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func shouldSkipFrontendStatic(requestPath string) bool {
|
||||
if requestPath == "/robots.txt" || requestPath == "/sitemap.xml" || strings.HasSuffix(requestPath, "/rss.xml") {
|
||||
return true
|
||||
}
|
||||
return hasPathPrefix(requestPath, "/api") ||
|
||||
hasPathPrefix(requestPath, "/file") ||
|
||||
hasPathPrefix(requestPath, "/mcp") ||
|
||||
requestPath == "/memos.api.v1" ||
|
||||
strings.HasPrefix(requestPath, "/memos.api.v1.")
|
||||
}
|
||||
|
||||
func setFrontendCacheHeaders(c *echo.Context, requestPath string) {
|
||||
if shouldServeFrontendHTML(requestPath) {
|
||||
c.Response().Header().Set(echo.HeaderCacheControl, frontendHTMLCacheControl)
|
||||
c.Response().Header().Set("Pragma", "no-cache")
|
||||
c.Response().Header().Set("Expires", "0")
|
||||
return
|
||||
}
|
||||
|
||||
cacheControl := frontendStaticAssetCacheControl
|
||||
if strings.HasPrefix(requestPath, "/assets/") {
|
||||
cacheControl = frontendHashedAssetCacheControl
|
||||
}
|
||||
c.Response().Header().Set(echo.HeaderCacheControl, cacheControl)
|
||||
}
|
||||
|
||||
func shouldServeFrontendHTML(requestPath string) bool {
|
||||
return requestPath == "/" || requestPath == "/index.html" || path.Ext(requestPath) == ""
|
||||
}
|
||||
|
||||
func hasPathPrefix(requestPath, prefix string) bool {
|
||||
return requestPath == prefix || strings.HasPrefix(requestPath, prefix+"/")
|
||||
}
|
||||
|
||||
func getFileSystem(path string) fs.FS {
|
||||
sub, err := fs.Sub(embeddedFiles, path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return sub
|
||||
}
|
||||
|
||||
func (s *FrontendService) registerRoutes(e *echo.Echo) {
|
||||
e.GET("/robots.txt", s.getRobotsTXT)
|
||||
e.GET("/sitemap.xml", s.getSitemapXML)
|
||||
}
|
||||
|
||||
func (s *FrontendService) getRobotsTXT(c *echo.Context) error {
|
||||
instanceURL, err := normalizeInstanceURL(s.Profile.InstanceURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
robotsTXT := strings.Join([]string{
|
||||
"User-agent: *",
|
||||
"Allow: /",
|
||||
"Host: " + instanceURL,
|
||||
"Sitemap: " + instanceURL + "/sitemap.xml",
|
||||
}, "\n")
|
||||
return c.String(http.StatusOK, robotsTXT)
|
||||
}
|
||||
|
||||
func (s *FrontendService) getSitemapXML(c *echo.Context) error {
|
||||
instanceURL, err := normalizeInstanceURL(s.Profile.InstanceURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
memos, err := s.Store.ListMemos(c.Request().Context(), &store.FindMemo{
|
||||
VisibilityList: []store.Visibility{store.Public},
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to list public memos for sitemap")
|
||||
}
|
||||
|
||||
urls := make([]sitemapURL, 0, len(memos))
|
||||
for _, memo := range memos {
|
||||
urls = append(urls, sitemapURL{
|
||||
Loc: instanceURL + "/memos/" + memo.UID,
|
||||
})
|
||||
}
|
||||
|
||||
return c.XML(http.StatusOK, sitemapURLSet{
|
||||
XMLNS: sitemapXMLNamespace,
|
||||
URLs: urls,
|
||||
})
|
||||
}
|
||||
|
||||
func normalizeInstanceURL(instanceURL string) (string, error) {
|
||||
instanceURL = strings.TrimRight(instanceURL, "/")
|
||||
if instanceURL == "" {
|
||||
return "", echo.NewHTTPError(http.StatusNotFound, "instance URL is not configured")
|
||||
}
|
||||
return instanceURL, nil
|
||||
}
|
||||
|
||||
type sitemapURLSet struct {
|
||||
XMLName xml.Name `xml:"urlset"`
|
||||
XMLNS string `xml:"xmlns,attr"`
|
||||
URLs []sitemapURL `xml:"url"`
|
||||
}
|
||||
|
||||
type sitemapURL struct {
|
||||
Loc string `xml:"loc"`
|
||||
}
|
||||
|
||||
//nolint:revive
|
||||
const sitemapXMLNamespace = "http://www.sitemaps.org/schemas/sitemap/0.9"
|
||||
@@ -0,0 +1,233 @@
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/usememos/memos/internal/profile"
|
||||
"github.com/usememos/memos/store"
|
||||
teststore "github.com/usememos/memos/store/test"
|
||||
)
|
||||
|
||||
func TestFrontendService_CacheHeaderRules(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
cacheControl string
|
||||
pragma string
|
||||
expires string
|
||||
}{
|
||||
{
|
||||
name: "root html is not stored",
|
||||
path: "/",
|
||||
cacheControl: frontendHTMLCacheControl,
|
||||
pragma: "no-cache",
|
||||
expires: "0",
|
||||
},
|
||||
{
|
||||
name: "index html is not stored",
|
||||
path: "/index.html",
|
||||
cacheControl: frontendHTMLCacheControl,
|
||||
pragma: "no-cache",
|
||||
expires: "0",
|
||||
},
|
||||
{
|
||||
name: "spa fallback html is not stored",
|
||||
path: "/memos/publicmemo",
|
||||
cacheControl: frontendHTMLCacheControl,
|
||||
pragma: "no-cache",
|
||||
expires: "0",
|
||||
},
|
||||
{
|
||||
name: "hashed asset is immutable",
|
||||
path: "/assets/index-deadbeef.js",
|
||||
cacheControl: frontendHashedAssetCacheControl,
|
||||
},
|
||||
{
|
||||
name: "stable root asset is revalidated after max age",
|
||||
path: "/logo.webp",
|
||||
cacheControl: frontendStaticAssetCacheControl,
|
||||
},
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, tt.path, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
|
||||
setFrontendCacheHeaders(c, tt.path)
|
||||
|
||||
require.Equal(t, tt.cacheControl, rec.Header().Get(echo.HeaderCacheControl))
|
||||
require.Equal(t, tt.pragma, rec.Header().Get("Pragma"))
|
||||
require.Equal(t, tt.expires, rec.Header().Get("Expires"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrontendService_StaticCacheHeaders(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testStore := teststore.NewTestingStore(ctx, t)
|
||||
|
||||
e := echo.New()
|
||||
NewFrontendService(&profile.Profile{}, testStore).Serve(ctx, e)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
cacheControl string
|
||||
pragma string
|
||||
expires string
|
||||
}{
|
||||
{
|
||||
name: "root html is not stored",
|
||||
path: "/",
|
||||
cacheControl: frontendHTMLCacheControl,
|
||||
pragma: "no-cache",
|
||||
expires: "0",
|
||||
},
|
||||
{
|
||||
name: "index html is not stored",
|
||||
path: "/index.html",
|
||||
cacheControl: frontendHTMLCacheControl,
|
||||
pragma: "no-cache",
|
||||
expires: "0",
|
||||
},
|
||||
{
|
||||
name: "spa fallback html is not stored",
|
||||
path: "/memos/publicmemo",
|
||||
cacheControl: frontendHTMLCacheControl,
|
||||
pragma: "no-cache",
|
||||
expires: "0",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, tt.path, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.Equal(t, tt.cacheControl, rec.Header().Get(echo.HeaderCacheControl))
|
||||
require.Equal(t, tt.pragma, rec.Header().Get("Pragma"))
|
||||
require.Equal(t, tt.expires, rec.Header().Get("Expires"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrontendService_MissingAssetDoesNotFallbackToIndex(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testStore := teststore.NewTestingStore(ctx, t)
|
||||
|
||||
e := echo.New()
|
||||
NewFrontendService(&profile.Profile{}, testStore).Serve(ctx, e)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/assets/missing.js", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusNotFound, rec.Code)
|
||||
}
|
||||
|
||||
func TestFrontendService_SkipsDynamicRoutes(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testStore := teststore.NewTestingStore(ctx, t)
|
||||
|
||||
e := echo.New()
|
||||
NewFrontendService(&profile.Profile{}, testStore).Serve(ctx, e)
|
||||
e.GET("/api/test", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "ok")
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/test", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.Empty(t, rec.Header().Get(echo.HeaderCacheControl))
|
||||
}
|
||||
|
||||
func TestFrontendService_RobotsTXT(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testStore := teststore.NewTestingStore(ctx, t)
|
||||
profile := &profile.Profile{
|
||||
InstanceURL: "https://demo.usememos.com/",
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
NewFrontendService(profile, testStore).Serve(ctx, e)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/robots.txt", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.Equal(t, "text/plain; charset=UTF-8", rec.Header().Get("Content-Type"))
|
||||
require.Equal(t, "User-agent: *\nAllow: /\nHost: https://demo.usememos.com\nSitemap: https://demo.usememos.com/sitemap.xml", rec.Body.String())
|
||||
}
|
||||
|
||||
func TestFrontendService_SitemapXML(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testStore := teststore.NewTestingStore(ctx, t)
|
||||
profile := &profile.Profile{
|
||||
InstanceURL: "https://demo.usememos.com",
|
||||
}
|
||||
|
||||
user, err := testStore.CreateUser(ctx, &store.User{
|
||||
Username: "sitemap-owner",
|
||||
Role: store.RoleUser,
|
||||
Email: "sitemap-owner@example.com",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = testStore.CreateMemo(ctx, &store.Memo{
|
||||
UID: "publicmemo",
|
||||
CreatorID: user.ID,
|
||||
Content: "public memo",
|
||||
Visibility: store.Public,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = testStore.CreateMemo(ctx, &store.Memo{
|
||||
UID: "privatememo",
|
||||
CreatorID: user.ID,
|
||||
Content: "private memo",
|
||||
Visibility: store.Private,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
e := echo.New()
|
||||
NewFrontendService(profile, testStore).Serve(ctx, e)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/sitemap.xml", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.Contains(t, rec.Header().Get("Content-Type"), "application/xml")
|
||||
require.Contains(t, rec.Body.String(), `<loc>https://demo.usememos.com/memos/publicmemo</loc>`)
|
||||
require.NotContains(t, rec.Body.String(), "privatememo")
|
||||
}
|
||||
|
||||
func TestFrontendService_SitemapRoutesRequireInstanceURL(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testStore := teststore.NewTestingStore(ctx, t)
|
||||
|
||||
e := echo.New()
|
||||
NewFrontendService(&profile.Profile{}, testStore).Serve(ctx, e)
|
||||
|
||||
for _, path := range []string{"/robots.txt", "/sitemap.xml"} {
|
||||
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusNotFound, rec.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user