2023-12-14 23:29:42 +08:00
|
|
|
package frontend
|
2022-07-10 09:02:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2023-12-15 07:32:49 +08:00
|
|
|
"fmt"
|
2022-07-10 09:02:56 +08:00
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
2023-12-14 23:29:42 +08:00
|
|
|
"strings"
|
2022-07-10 09:02:56 +08:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2022-07-15 21:25:29 +08:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-10-26 09:02:50 +08:00
|
|
|
"github.com/usememos/memos/internal/util"
|
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
|
|
|
|
|
2023-12-14 23:29:42 +08:00
|
|
|
//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) {
|
2022-09-09 00:50:58 +08:00
|
|
|
// Use echo static middleware to serve the built dist folder
|
|
|
|
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
|
2022-07-15 21:25:29 +08:00
|
|
|
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
2023-02-12 17:29:23 +08:00
|
|
|
Skipper: defaultAPIRequestSkipper,
|
2022-07-15 21:25:29 +08:00
|
|
|
HTML5: true,
|
2022-09-09 00:50:58 +08:00
|
|
|
Filesystem: getFileSystem("dist"),
|
|
|
|
}))
|
|
|
|
|
2023-01-13 07:06:15 +08:00
|
|
|
assetsGroup := e.Group("assets")
|
2023-12-14 23:29:42 +08:00
|
|
|
assetsGroup.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
|
|
|
Skipper: defaultAPIRequestSkipper,
|
|
|
|
Level: 5,
|
|
|
|
}))
|
2023-01-13 07:06:15 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2023-01-13 07:06:15 +08:00
|
|
|
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"),
|
2022-07-15 21:25:29 +08:00
|
|
|
}))
|
2023-12-14 23:29:42 +08:00
|
|
|
|
2023-12-15 07:32:49 +08:00
|
|
|
s.registerRoutes(e)
|
2023-12-14 23:29:42 +08:00
|
|
|
}
|
|
|
|
|
2023-12-15 07:32:49 +08:00
|
|
|
func (s *FrontendService) registerRoutes(e *echo.Echo) {
|
2023-12-14 23:29:42 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
if memo.Visibility != store.Public {
|
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))
|
2023-12-14 23:29:42 +08:00
|
|
|
return c.HTML(http.StatusOK, indexHTML)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-15 08:12:10 +08:00
|
|
|
func generateMemoMetadata(memo *store.Memo, creator *store.User) string {
|
2023-12-15 07:32:49 +08:00
|
|
|
metadataList := []string{
|
|
|
|
fmt.Sprintf(`<meta name="description" content="%s" />`, memo.Content),
|
2023-12-15 08:12:10 +08:00
|
|
|
fmt.Sprintf(`<meta property="og:title" content="%s" />`, fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username)),
|
2023-12-15 07:32:49 +08:00
|
|
|
fmt.Sprintf(`<meta property="og:description" content="%s" />`, memo.Content),
|
|
|
|
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-15 08:12:10 +08:00
|
|
|
fmt.Sprintf(`<meta name="twitter:title" content="%s" />`, fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username)),
|
2023-12-15 07:32:49 +08:00
|
|
|
fmt.Sprintf(`<meta name="twitter:description" content="%s" />`, memo.Content),
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-12-14 23:29:42 +08:00
|
|
|
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")
|
|
|
|
}
|