chore: use echo static middleware to serve dist

This commit is contained in:
boojack 2022-07-15 21:25:29 +08:00
parent 6053df050c
commit 1b50ab5dca
2 changed files with 5 additions and 32 deletions

19
.gitignore vendored
View file

@ -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

View file

@ -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(),
}))
}