mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
2 years ago
|
package frontend
|
||
3 years ago
|
|
||
|
import (
|
||
|
"embed"
|
||
|
"io/fs"
|
||
|
"net/http"
|
||
2 years ago
|
"strings"
|
||
3 years ago
|
|
||
|
"github.com/labstack/echo/v4"
|
||
3 years ago
|
"github.com/labstack/echo/v4/middleware"
|
||
2 years ago
|
|
||
2 years ago
|
"github.com/usememos/memos/internal/util"
|
||
3 years ago
|
)
|
||
|
|
||
|
//go:embed dist
|
||
|
var embeddedFiles embed.FS
|
||
|
|
||
2 years ago
|
//go:embed dist/index.html
|
||
|
var rawIndexHTML string
|
||
3 years ago
|
|
||
2 years ago
|
func Serve(e *echo.Echo) {
|
||
3 years ago
|
// Use echo static middleware to serve the built dist folder
|
||
|
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
|
||
3 years ago
|
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||
2 years ago
|
Skipper: defaultAPIRequestSkipper,
|
||
3 years ago
|
HTML5: true,
|
||
3 years ago
|
Filesystem: getFileSystem("dist"),
|
||
|
}))
|
||
|
|
||
3 years ago
|
assetsGroup := e.Group("assets")
|
||
2 years ago
|
assetsGroup.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
||
|
Skipper: defaultAPIRequestSkipper,
|
||
|
Level: 5,
|
||
|
}))
|
||
3 years ago
|
assetsGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||
3 years ago
|
return func(c echo.Context) error {
|
||
|
c.Response().Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
|
||
|
return next(c)
|
||
|
}
|
||
|
})
|
||
3 years ago
|
assetsGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||
2 years ago
|
Skipper: defaultAPIRequestSkipper,
|
||
3 years ago
|
HTML5: true,
|
||
|
Filesystem: getFileSystem("dist/assets"),
|
||
3 years ago
|
}))
|
||
2 years ago
|
|
||
|
registerRoutes(e)
|
||
|
}
|
||
|
|
||
|
func registerRoutes(e *echo.Echo) {
|
||
|
e.GET("/m/:memoID", func(c echo.Context) error {
|
||
|
indexHTML := strings.ReplaceAll(rawIndexHTML, "<!-- memos.metadata -->", "<meta name=\"memos-memo-id\" content=\""+c.Param("memoID")+"\">"+"\n")
|
||
|
return c.HTML(http.StatusOK, indexHTML)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func getFileSystem(path string) http.FileSystem {
|
||
|
fs, err := fs.Sub(embeddedFiles, path)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return http.FS(fs)
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
|
func defaultAPIRequestSkipper(c echo.Context) bool {
|
||
|
path := c.Request().URL.Path
|
||
|
return util.HasPrefixes(path, "/api", "/memos.api.v2")
|
||
|
}
|