memos/server/frontend/frontend.go

209 lines
7 KiB
Go
Raw Normal View History

package frontend
2022-07-10 09:02:56 +08:00
import (
"embed"
2023-12-15 07:32:49 +08:00
"fmt"
2023-12-17 23:37:00 +08:00
"html/template"
2022-07-10 09:02:56 +08:00
"io/fs"
"net/http"
"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-15 20:18:01 +08:00
v1 "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
)
//go:embed dist
var embeddedFiles embed.FS
//go:embed dist/index.html
var rawIndexHTML string
2022-07-10 09:02:56 +08:00
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,
}
}
func (s *FrontendService) Serve(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-02-12 17:29:23 +08:00
Skipper: defaultAPIRequestSkipper,
HTML5: true,
2022-09-09 00:50:58 +08:00
Filesystem: getFileSystem("dist"),
}))
assetsGroup := e.Group("assets")
assetsGroup.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Skipper: defaultAPIRequestSkipper,
Level: 5,
}))
assetsGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
2022-09-09 00:50:58 +08:00
return func(c echo.Context) error {
c.Response().Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
return next(c)
}
})
assetsGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
2023-02-12 17:29:23 +08:00
Skipper: defaultAPIRequestSkipper,
2022-09-09 00:50:58 +08:00
HTML5: true,
Filesystem: getFileSystem("dist/assets"),
}))
2023-12-15 07:32:49 +08:00
s.registerRoutes(e)
}
2023-12-15 07:32:49 +08:00
func (s *FrontendService) registerRoutes(e *echo.Echo) {
2023-12-15 20:18:01 +08:00
e.GET("/robots.txt", func(c echo.Context) error {
ctx := c.Request().Context()
instanceURLSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
Name: v1.SystemSettingInstanceURLName.String(),
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get instance URL system setting").SetInternal(err)
}
if instanceURLSetting == nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Instance URL system setting is not set")
}
instanceURL := instanceURLSetting.Value
2023-12-23 08:35:54 +08:00
if instanceURL == "" {
return echo.NewHTTPError(http.StatusInternalServerError, "Instance URL system setting is not set")
}
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()
instanceURLSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
Name: v1.SystemSettingInstanceURLName.String(),
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get instance URL system setting").SetInternal(err)
}
if instanceURLSetting == nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Instance URL system setting is not set")
}
instanceURL := instanceURLSetting.Value
2023-12-23 08:35:54 +08:00
if instanceURL == "" {
return echo.NewHTTPError(http.StatusInternalServerError, "Instance URL system setting is not set")
}
2023-12-15 20:18:01 +08:00
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)))
}
// Append user list.
userList, err := s.Store.ListUsers(ctx, &store.FindUser{})
if err != nil {
return err
}
for _, user := range userList {
urlsets = append(urlsets, fmt.Sprintf(`<url><loc>%s</loc></url>`, fmt.Sprintf("%s/u/%s", instanceURL, user.Username)))
}
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))
})
e.GET("/m/:memoID", func(c echo.Context) error {
2023-12-15 07:32:49 +08:00
ctx := c.Request().Context()
memoID, err := util.ConvertStringToInt32(c.Param("memoID"))
if err != nil {
2023-12-15 08:12:10 +08:00
// Redirect to `index.html` if any error occurs.
return c.HTML(http.StatusOK, rawIndexHTML)
2023-12-15 07:32:49 +08:00
}
memo, err := s.Store.GetMemo(ctx, &store.FindMemo{
ID: &memoID,
})
if err != nil {
2023-12-15 08:12:10 +08:00
return c.HTML(http.StatusOK, rawIndexHTML)
2023-12-15 07:32:49 +08:00
}
if memo == nil {
2023-12-15 08:12:10 +08:00
return c.HTML(http.StatusOK, rawIndexHTML)
2023-12-15 07:32:49 +08:00
}
2023-12-15 08:12:10 +08:00
creator, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &memo.CreatorID,
})
if err != nil {
return c.HTML(http.StatusOK, rawIndexHTML)
}
// Inject memo metadata into `index.html`.
indexHTML := strings.ReplaceAll(rawIndexHTML, "<!-- memos.metadata -->", generateMemoMetadata(memo, creator))
return c.HTML(http.StatusOK, indexHTML)
})
}
2023-12-15 08:12:10 +08:00
func generateMemoMetadata(memo *store.Memo, creator *store.User) string {
description := ""
2023-12-15 19:13:56 +08:00
if memo.Visibility == store.Private {
description = "This memo is private."
} else if memo.Visibility == store.Protected {
description = "This memo is protected."
} else {
tokens := tokenizer.Tokenize(memo.Content)
nodes, _ := parser.Parse(tokens)
2023-12-16 11:57:36 +08:00
description = renderer.NewStringRenderer().Render(nodes)
if len(description) == 0 {
description = memo.Content
}
2023-12-16 10:38:05 +08:00
if len(description) > 200 {
description = description[:200] + "..."
}
2023-12-15 19:13:56 +08:00
}
2023-12-15 07:32:49 +08:00
metadataList := []string{
2023-12-17 23:37:00 +08:00
fmt.Sprintf(`<meta name="description" content="%s" />`, template.HTMLEscapeString(description)),
fmt.Sprintf(`<meta property="og:title" content="%s" />`, template.HTMLEscapeString(fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username))),
fmt.Sprintf(`<meta property="og:description" content="%s" />`, template.HTMLEscapeString(description)),
2023-12-15 07:32:49 +08:00
fmt.Sprintf(`<meta property="og:image" content="%s" />`, "https://www.usememos.com/logo.png"),
`<meta property="og:type" content="website" />`,
// Twitter related metadata.
2023-12-17 23:37:00 +08:00
fmt.Sprintf(`<meta name="twitter:title" content="%s" />`, template.HTMLEscapeString(fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username))),
fmt.Sprintf(`<meta name="twitter:description" content="%s" />`, template.HTMLEscapeString(description)),
2023-12-15 07:32:49 +08:00
fmt.Sprintf(`<meta name="twitter:image" content="%s" />`, "https://www.usememos.com/logo.png"),
`<meta name="twitter:card" content="summary" />`,
}
return strings.Join(metadataList, "\n")
}
func getFileSystem(path string) http.FileSystem {
fs, err := fs.Sub(embeddedFiles, path)
if err != nil {
panic(err)
}
return http.FS(fs)
2022-07-10 09:02:56 +08:00
}
2023-09-17 19:20:03 +08:00
func defaultAPIRequestSkipper(c echo.Context) bool {
path := c.Request().URL.Path
return util.HasPrefixes(path, "/api", "/memos.api.v2")
}