mirror of https://github.com/usememos/memos
feat: use go embed
parent
48d8c6ee0f
commit
46d7ecca88
@ -0,0 +1,13 @@
|
||||
<!-- 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>Memos</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>No frontend embeded.</p>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,37 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
//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 {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return http.FS(fs)
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
Before Width: | Height: | Size: 121 B After Width: | Height: | Size: 121 B |
Loading…
Reference in New Issue