memos/server/frontend/frontend.go

180 lines
5.6 KiB
Go
Raw Normal View History

package frontend
2022-07-10 09:02:56 +08:00
import (
2023-12-23 17:58:49 +08:00
"context"
2023-12-15 07:32:49 +08:00
"fmt"
2022-07-10 09:02:56 +08:00
"net/http"
2023-12-23 14:13:40 +08:00
"os"
"strings"
2022-07-10 09:02:56 +08:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
2023-09-17 22:55:13 +08:00
2023-12-23 17:58:49 +08:00
apiv1 "github.com/usememos/memos/api/v1"
"github.com/usememos/memos/internal/util"
"github.com/usememos/memos/plugin/gomark/parser"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
2023-12-16 11:57:36 +08:00
"github.com/usememos/memos/plugin/gomark/renderer"
2023-12-15 07:32:49 +08:00
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
2022-07-10 09:02:56 +08:00
)
2023-12-23 17:58:49 +08:00
const (
// maxMetadataDescriptionLength is the maximum length of metadata description.
maxMetadataDescriptionLength = 256
)
2023-12-15 07:32:49 +08:00
type FrontendService struct {
Profile *profile.Profile
Store *store.Store
}
func NewFrontendService(profile *profile.Profile, store *store.Store) *FrontendService {
return &FrontendService{
Profile: profile,
Store: store,
}
}
2023-12-23 17:58:49 +08:00
func (s *FrontendService) Serve(ctx context.Context, e *echo.Echo) {
2023-12-23 08:35:54 +08:00
// Use echo static middleware to serve the built dist folder.
2022-09-09 00:50:58 +08:00
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
2023-12-23 17:58:49 +08:00
Root: "dist",
HTML5: true,
2023-12-23 17:04:52 +08:00
Skipper: func(c echo.Context) bool {
return util.HasPrefixes(c.Path(), "/api", "/memos.api.v2", "/robots.txt", "/sitemap.xml", "/m/:memoID")
},
}))
2023-12-15 07:32:49 +08:00
s.registerRoutes(e)
2023-12-23 17:58:49 +08:00
s.registerFileRoutes(ctx, e)
}
2023-12-15 07:32:49 +08:00
func (s *FrontendService) registerRoutes(e *echo.Echo) {
2023-12-23 17:04:52 +08:00
rawIndexHTML := getRawIndexHTML()
2023-12-23 14:13:40 +08:00
2023-12-23 17:58:49 +08:00
e.GET("/m/:memoID", func(c echo.Context) error {
2023-12-15 20:18:01 +08:00
ctx := c.Request().Context()
2023-12-23 17:58:49 +08:00
memoID, err := util.ConvertStringToInt32(c.Param("memoID"))
if err != nil {
// Redirect to `index.html` if any error occurs.
return c.HTML(http.StatusOK, rawIndexHTML)
}
memo, err := s.Store.GetMemo(ctx, &store.FindMemo{
ID: &memoID,
2023-12-15 20:18:01 +08:00
})
if err != nil {
2023-12-23 17:58:49 +08:00
return c.HTML(http.StatusOK, rawIndexHTML)
2023-12-15 20:18:01 +08:00
}
2023-12-23 17:58:49 +08:00
if memo == nil {
return c.HTML(http.StatusOK, rawIndexHTML)
2023-12-15 20:18:01 +08:00
}
2023-12-23 17:58:49 +08:00
creator, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &memo.CreatorID,
})
if err != nil {
return c.HTML(http.StatusOK, rawIndexHTML)
2023-12-23 08:35:54 +08:00
}
2023-12-23 17:58:49 +08:00
// Inject memo metadata into `index.html`.
2023-12-29 08:28:17 +08:00
indexHTML := strings.ReplaceAll(rawIndexHTML, "<!-- memos.metadata.head -->", generateMemoMetadata(memo, creator).String())
indexHTML = strings.ReplaceAll(indexHTML, "<!-- memos.metadata.body -->", fmt.Sprintf("<!-- memos.memo.%d -->", memo.ID))
2023-12-23 17:58:49 +08:00
return c.HTML(http.StatusOK, indexHTML)
})
}
func (s *FrontendService) registerFileRoutes(ctx context.Context, e *echo.Echo) {
instanceURLSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
Name: apiv1.SystemSettingInstanceURLName.String(),
})
if err != nil || instanceURLSetting == nil {
return
}
instanceURL := instanceURLSetting.Value
if instanceURL == "" {
return
}
e.GET("/robots.txt", func(c echo.Context) error {
2023-12-15 20:18:01 +08:00
robotsTxt := fmt.Sprintf(`User-agent: *
Allow: /
Host: %s
Sitemap: %s/sitemap.xml`, instanceURL, instanceURL)
return c.String(http.StatusOK, robotsTxt)
})
e.GET("/sitemap.xml", func(c echo.Context) error {
ctx := c.Request().Context()
urlsets := []string{}
// Append memo list.
memoList, err := s.Store.ListMemos(ctx, &store.FindMemo{
VisibilityList: []store.Visibility{store.Public},
})
if err != nil {
return err
}
for _, memo := range memoList {
urlsets = append(urlsets, fmt.Sprintf(`<url><loc>%s</loc></url>`, fmt.Sprintf("%s/m/%d", instanceURL, memo.ID)))
}
sitemap := fmt.Sprintf(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">%s</urlset>`, strings.Join(urlsets, "\n"))
return c.XMLBlob(http.StatusOK, []byte(sitemap))
})
}
2023-12-29 08:19:32 +08:00
func generateMemoMetadata(memo *store.Memo, creator *store.User) *Metadata {
metadata := getDefaultMetadata()
metadata.Title = fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username)
if memo.Visibility == store.Public {
tokens := tokenizer.Tokenize(memo.Content)
nodes, _ := parser.Parse(tokens)
2023-12-29 08:19:32 +08:00
description := renderer.NewStringRenderer().Render(nodes)
if len(description) == 0 {
description = memo.Content
}
2023-12-23 17:58:49 +08:00
if len(description) > maxMetadataDescriptionLength {
description = description[:maxMetadataDescriptionLength] + "..."
}
2023-12-29 08:19:32 +08:00
metadata.Description = description
2023-12-15 19:13:56 +08:00
}
2023-12-29 08:19:32 +08:00
return metadata
2023-12-15 07:32:49 +08:00
}
2023-12-23 17:04:52 +08:00
func getRawIndexHTML() string {
bytes, _ := os.ReadFile("dist/index.html")
return string(bytes)
2023-09-17 19:20:03 +08:00
}
2023-12-29 08:19:32 +08:00
type Metadata struct {
Title string
Description string
ImageURL string
}
func getDefaultMetadata() *Metadata {
return &Metadata{
Title: "Memos",
Description: "A privacy-first, lightweight note-taking service. Easily capture and share your great thoughts.",
ImageURL: "/logo.webp",
2023-12-29 08:19:32 +08:00
}
}
func (m *Metadata) String() string {
metadataList := []string{
fmt.Sprintf(`<meta name="description" content="%s" />`, m.Description),
fmt.Sprintf(`<meta property="og:title" content="%s" />`, m.Title),
fmt.Sprintf(`<meta property="og:description" content="%s" />`, m.Description),
fmt.Sprintf(`<meta property="og:image" content="%s" />`, m.ImageURL),
`<meta property="og:type" content="website" />`,
// Twitter related fields.
fmt.Sprintf(`<meta property="twitter:title" content="%s" />`, m.Title),
fmt.Sprintf(`<meta property="twitter:description" content="%s" />`, m.Description),
fmt.Sprintf(`<meta property="twitter:image" content="%s" />`, m.ImageURL),
`<meta name="twitter:card" content="summary" />`,
`<meta name="twitter:creator" content="memos" />`,
}
return strings.Join(metadataList, "\n")
}