2022-07-10 09:02:56 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2022-07-15 21:25:29 +08:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2022-07-10 09:02:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed dist
|
|
|
|
var embeddedFiles embed.FS
|
|
|
|
|
2022-09-09 00:50:58 +08:00
|
|
|
func getFileSystem(path string) http.FileSystem {
|
|
|
|
fs, err := fs.Sub(embeddedFiles, path)
|
2022-07-10 09:02:56 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.FS(fs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func embedFrontend(e *echo.Echo) {
|
2022-09-09 00:50:58 +08:00
|
|
|
// Use echo static middleware to serve the built dist folder
|
|
|
|
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
|
2022-07-15 21:25:29 +08:00
|
|
|
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
2023-02-12 17:29:23 +08:00
|
|
|
Skipper: defaultAPIRequestSkipper,
|
2022-07-15 21:25:29 +08:00
|
|
|
HTML5: true,
|
2022-09-09 00:50:58 +08:00
|
|
|
Filesystem: getFileSystem("dist"),
|
|
|
|
}))
|
|
|
|
|
2023-01-13 07:06:15 +08:00
|
|
|
assetsGroup := e.Group("assets")
|
|
|
|
assetsGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
2022-09-09 00:50:58 +08:00
|
|
|
return func(c echo.Context) error {
|
|
|
|
c.Response().Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2023-01-13 07:06:15 +08:00
|
|
|
assetsGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
2023-02-12 17:29:23 +08:00
|
|
|
Skipper: defaultAPIRequestSkipper,
|
2022-09-09 00:50:58 +08:00
|
|
|
HTML5: true,
|
|
|
|
Filesystem: getFileSystem("dist/assets"),
|
2022-07-15 21:25:29 +08:00
|
|
|
}))
|
2022-07-10 09:02:56 +08:00
|
|
|
}
|