1
0
Fork 0
forked from Kispi/Core
HGOE-SaS/server/pkg/middlewares/serve-spa.go
2023-07-03 11:38:34 +02:00

24 lines
721 B
Go

package middlewares
import (
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pterm/pterm"
)
func ServeSPA(webAppPath string) func(e *core.ServeEvent) error {
return func(e *core.ServeEvent) error {
pterm.Info.Print("Serving SPA from ", webAppPath)
subFs := echo.MustSubFS(e.Router.Filesystem, webAppPath)
e.Router.GET("/*", apis.StaticDirectoryHandler(subFs, false))
originalErrorHandler := e.Router.HTTPErrorHandler
e.Router.HTTPErrorHandler = func(c echo.Context, err error) {
if c.Path() == "/*" && err == echo.ErrNotFound {
err = c.FileFS("index.html", subFs)
}
originalErrorHandler(c, err)
}
return nil
}
}