frontend.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package frontend
  2. import (
  3. "context"
  4. "embed"
  5. "io/fs"
  6. "net/http"
  7. "github.com/labstack/echo/v4"
  8. "github.com/labstack/echo/v4/middleware"
  9. "github.com/usememos/memos/internal/util"
  10. "github.com/usememos/memos/server/profile"
  11. "github.com/usememos/memos/store"
  12. )
  13. //go:embed dist/*
  14. var embeddedFiles embed.FS
  15. type FrontendService struct {
  16. Profile *profile.Profile
  17. Store *store.Store
  18. }
  19. func NewFrontendService(profile *profile.Profile, store *store.Store) *FrontendService {
  20. return &FrontendService{
  21. Profile: profile,
  22. Store: store,
  23. }
  24. }
  25. func (*FrontendService) Serve(_ context.Context, e *echo.Echo) {
  26. skipper := func(c echo.Context) bool {
  27. return util.HasPrefixes(c.Path(), "/api", "/memos.api.v1")
  28. }
  29. // Route to serve the assets folder without HTML5 fallback.
  30. e.Group("/assets").Use(middleware.GzipWithConfig(middleware.GzipConfig{
  31. Level: 5,
  32. }), func(next echo.HandlerFunc) echo.HandlerFunc {
  33. return func(c echo.Context) error {
  34. c.Response().Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
  35. return next(c)
  36. }
  37. }, middleware.StaticWithConfig(middleware.StaticConfig{
  38. Filesystem: getFileSystem("dist/assets"),
  39. HTML5: false, // Disable fallback to index.html
  40. }))
  41. // Route to serve the main app with HTML5 fallback for SPA behavior.
  42. e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
  43. Filesystem: getFileSystem("dist"),
  44. HTML5: true, // Enable fallback to index.html
  45. Skipper: skipper,
  46. }))
  47. }
  48. func getFileSystem(path string) http.FileSystem {
  49. fs, err := fs.Sub(embeddedFiles, path)
  50. if err != nil {
  51. panic(err)
  52. }
  53. return http.FS(fs)
  54. }