chore: update embed frontend

This commit is contained in:
Steven
2023-12-24 01:05:15 +08:00
parent 91708da5fc
commit 88606e0a0c
11 changed files with 218 additions and 185 deletions

View File

@ -1,13 +0,0 @@
<!-- THIS FILE IS A PLACEHOLDER AND SHOULD NOT BE CHANGED -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Shortify</title>
</head>
<body>
<p>No frontend embeded.</p>
</body>
</html>

View File

@ -1,11 +1,11 @@
package server
import (
"embed"
"context"
"fmt"
"html/template"
"io/fs"
"net/http"
"os"
"strings"
"github.com/labstack/echo/v4"
@ -17,12 +17,6 @@ import (
"github.com/yourselfhosted/slash/store"
)
//go:embed dist
var embeddedFiles embed.FS
//go:embed dist/index.html
var rawIndexHTML string
type FrontendService struct {
Profile *profile.Profile
Store *store.Store
@ -35,43 +29,74 @@ func NewFrontendService(profile *profile.Profile, store *store.Store) *FrontendS
}
}
func (s *FrontendService) Serve(e *echo.Echo) {
func (s *FrontendService) Serve(ctx context.Context, e *echo.Echo) {
// Use echo static middleware to serve the built dist folder.
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Skipper: defaultRequestSkipper,
HTML5: true,
Filesystem: getFileSystem("dist"),
HTML5: true,
Root: "dist",
Skipper: func(c echo.Context) bool {
return util.HasPrefixes(c.Path(), "/api", "/slash.api.v2", "/robots.txt", "/sitemap.xml", "/s/:shortcutName")
},
}))
assetsGroup := e.Group("assets")
assetsGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
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{
Skipper: defaultRequestSkipper,
HTML5: true,
Filesystem: getFileSystem("dist/assets"),
}))
s.registerRoutes(e)
s.registerFileRoutes(ctx, e)
}
func (s *FrontendService) registerRoutes(e *echo.Echo) {
e.GET("/robots.txt", func(c echo.Context) error {
rawIndexHTML := getRawIndexHTML()
e.GET("/s/:shortcutName", func(c echo.Context) error {
ctx := c.Request().Context()
instanceURLSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_INSTANCE_URL,
shortcutName := c.Param("shortcutName")
shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{
Name: &shortcutName,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get instance URL system setting").SetInternal(err)
return c.HTML(http.StatusOK, rawIndexHTML)
}
if instanceURLSetting == nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Instance URL system setting is not set")
if shortcut == nil {
return c.HTML(http.StatusOK, rawIndexHTML)
}
instanceURL := instanceURLSetting.GetInstanceUrl()
// Inject shortcut metadata into `index.html`.
indexHTML := strings.ReplaceAll(rawIndexHTML, "<!-- slash.metadata -->", generateShortcutMetadata(shortcut))
return c.HTML(http.StatusOK, indexHTML)
})
e.GET("/c/:collectionName", func(c echo.Context) error {
ctx := c.Request().Context()
collectionName := c.Param("collectionName")
collection, err := s.Store.GetCollection(ctx, &store.FindCollection{
Name: &collectionName,
})
if err != nil {
return c.HTML(http.StatusOK, rawIndexHTML)
}
if collection == nil {
return c.HTML(http.StatusOK, rawIndexHTML)
}
// Inject collection metadata into `index.html`.
indexHTML := strings.ReplaceAll(rawIndexHTML, "<!-- slash.metadata -->", generateCollectionMetadata(collection))
return c.HTML(http.StatusOK, indexHTML)
})
}
func (s *FrontendService) registerFileRoutes(ctx context.Context, e *echo.Echo) {
instanceURLSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_INSTANCE_URL,
})
if err != nil || instanceURLSetting == nil {
return
}
instanceURL := instanceURLSetting.GetInstanceUrl()
if instanceURL == "" {
return
}
e.GET("/robots.txt", func(c echo.Context) error {
robotsTxt := fmt.Sprintf(`User-agent: *
Allow: /
Host: %s
@ -80,18 +105,6 @@ Sitemap: %s/sitemap.xml`, instanceURL, instanceURL)
})
e.GET("/sitemap.xml", func(c echo.Context) error {
ctx := c.Request().Context()
instanceURLSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_INSTANCE_URL,
})
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.GetInstanceUrl()
urlsets := []string{}
// Append shortcut list.
shortcuts, err := s.Store.ListShortcuts(ctx, &store.FindShortcut{
@ -117,29 +130,10 @@ Sitemap: %s/sitemap.xml`, instanceURL, instanceURL)
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("/s/:shortcutName", func(c echo.Context) error {
ctx := c.Request().Context()
shortcutName := c.Param("shortcutName")
shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{
Name: &shortcutName,
})
if err != nil {
return c.HTML(http.StatusOK, rawIndexHTML)
}
if shortcut == nil {
return c.HTML(http.StatusOK, rawIndexHTML)
}
// Inject shortcut metadata into `index.html`.
indexHTML := strings.ReplaceAll(rawIndexHTML, "<!-- slash.metadata -->", generateShortcutMetadata(shortcut))
return c.HTML(http.StatusOK, indexHTML)
})
}
func generateShortcutMetadata(shortcut *storepb.Shortcut) string {
metadataList := []string{
fmt.Sprintf(`<title>%s</title>`, template.HTMLEscapeString(shortcut.OgMetadata.Title)),
fmt.Sprintf(`<meta name="description" content="%s" />`, template.HTMLEscapeString(shortcut.OgMetadata.Description)),
fmt.Sprintf(`<meta property="og:title" content="%s" />`, template.HTMLEscapeString(shortcut.OgMetadata.Title)),
fmt.Sprintf(`<meta property="og:description" content="%s" />`, template.HTMLEscapeString(shortcut.OgMetadata.Description)),
@ -155,16 +149,20 @@ func generateShortcutMetadata(shortcut *storepb.Shortcut) string {
return strings.Join(metadataList, "\n")
}
func getFileSystem(path string) http.FileSystem {
fs, err := fs.Sub(embeddedFiles, path)
if err != nil {
panic(err)
func generateCollectionMetadata(collection *storepb.Collection) string {
metadataList := []string{
fmt.Sprintf(`<meta name="description" content="%s" />`, template.HTMLEscapeString(collection.Description)),
fmt.Sprintf(`<meta property="og:title" content="%s" />`, template.HTMLEscapeString(collection.Title)),
fmt.Sprintf(`<meta property="og:description" content="%s" />`, template.HTMLEscapeString(collection.Description)),
`<meta property="og:type" content="website" />`,
// Twitter related metadata.
fmt.Sprintf(`<meta name="twitter:title" content="%s" />`, template.HTMLEscapeString(collection.Title)),
fmt.Sprintf(`<meta name="twitter:description" content="%s" />`, template.HTMLEscapeString(collection.Description)),
}
return http.FS(fs)
return strings.Join(metadataList, "\n")
}
func defaultRequestSkipper(c echo.Context) bool {
path := c.Path()
return util.HasPrefixes(path, "/api/")
func getRawIndexHTML() string {
bytes, _ := os.ReadFile("dist/index.html")
return string(bytes)
}

View File

@ -90,7 +90,7 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
// Serve frontend.
frontendService := NewFrontendService(profile, store)
frontendService.Serve(e)
frontendService.Serve(ctx, e)
// In dev mode, we'd like to set the const secret key to make signin session persistence.
secret := "slash"