From 1b50ab5dca495a0f1aee88a2abae7d934218f0b8 Mon Sep 17 00:00:00 2001 From: boojack Date: Fri, 15 Jul 2022 21:25:29 +0800 Subject: [PATCH] chore: use echo static middleware to serve dist --- .gitignore | 19 ------------------- server/embed_frontend.go | 18 +++++------------- 2 files changed, 5 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 0f555301..3ddbd693 100644 --- a/.gitignore +++ b/.gitignore @@ -1,29 +1,10 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, built with `go test -c` -*.test - -# Temp output -*.out -*.log -tmp - # Air (hot reload) generated .air # Frontend asset web/dist -# Dev database -data - # build folder -build memos-build .DS_Store \ No newline at end of file diff --git a/server/embed_frontend.go b/server/embed_frontend.go index 3efc4e95..e232ed1a 100644 --- a/server/embed_frontend.go +++ b/server/embed_frontend.go @@ -6,14 +6,12 @@ import ( "net/http" "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" ) //go:embed dist var embeddedFiles embed.FS -//go:embed dist/index.html -var indexContent string - func getFileSystem() http.FileSystem { fs, err := fs.Sub(embeddedFiles, "dist") if err != nil { @@ -24,14 +22,8 @@ func getFileSystem() http.FileSystem { } func embedFrontend(e *echo.Echo) { - // Catch-all route to return index.html, this is to prevent 404 when accessing non-root url. - // See https://stackoverflow.com/questions/27928372/react-router-urls-dont-work-when-refreshing-or-writing-manually - e.GET("/*", func(c echo.Context) error { - return c.HTML(http.StatusOK, indexContent) - }) - - assetHandler := http.FileServer(getFileSystem()) - e.GET("/assets/*", echo.WrapHandler(assetHandler)) - e.GET("/icons/*", echo.WrapHandler(assetHandler)) - e.GET("/favicon.svg", echo.WrapHandler(assetHandler)) + e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ + HTML5: true, + Filesystem: getFileSystem(), + })) }