1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-28 10:03:42 +02:00

refactored the admin ui routes registration for better sub-path deployment support

This commit is contained in:
Gani Georgiev 2022-08-01 20:37:51 +03:00
parent 16fa099685
commit 30d1b9f358

View File

@ -18,6 +18,8 @@ import (
"github.com/spf13/cast" "github.com/spf13/cast"
) )
const trailedAdminPath = "/_/"
// InitApi creates a configured echo instance with registered // InitApi creates a configured echo instance with registered
// system and app specific routes and middlewares. // system and app specific routes and middlewares.
func InitApi(app core.App) (*echo.Echo, error) { func InitApi(app core.App) (*echo.Echo, error) {
@ -25,7 +27,12 @@ func InitApi(app core.App) (*echo.Echo, error) {
e.Debug = app.IsDebug() e.Debug = app.IsDebug()
// default middlewares // default middlewares
e.Pre(middleware.RemoveTrailingSlash()) e.Pre(middleware.RemoveTrailingSlashWithConfig(middleware.RemoveTrailingSlashConfig{
Skipper: func(c echo.Context) bool {
// ignore Admin UI route(s)
return strings.HasPrefix(c.Request().URL.Path, trailedAdminPath)
},
}))
e.Use(middleware.Recover()) e.Use(middleware.Recover())
e.Use(middleware.Secure()) e.Use(middleware.Secure())
e.Use(LoadAuthContext(app)) e.Use(LoadAuthContext(app))
@ -128,21 +135,20 @@ func StaticDirectoryHandler(fileSystem fs.FS, disablePathUnescaping bool) echo.H
// bindStaticAdminUI registers the endpoints that serves the static admin UI. // bindStaticAdminUI registers the endpoints that serves the static admin UI.
func bindStaticAdminUI(app core.App, e *echo.Echo) error { func bindStaticAdminUI(app core.App, e *echo.Echo) error {
// serves /ui/dist/index.html file // redirect to trailing slash to ensure that relative urls will still work properly
// (explicit route is used to avoid conflicts with `RemoveTrailingSlash` middleware) e.GET(
e.FileFS( strings.TrimRight(trailedAdminPath, "/"),
"/_", func(c echo.Context) error {
"index.html", return c.Redirect(http.StatusTemporaryRedirect, trailedAdminPath)
ui.DistIndexHTML, },
middleware.Gzip(),
installerRedirect(app),
) )
// serves static files from the /ui/dist directory // serves static files from the /ui/dist directory
// (similar to echo.StaticFS but with gzip middleware enabled) // (similar to echo.StaticFS but with gzip middleware enabled)
e.GET( e.GET(
"/_/*", trailedAdminPath+"*",
StaticDirectoryHandler(ui.DistDirFS, false), echo.StaticDirectoryHandler(ui.DistDirFS, false),
installerRedirect(app),
middleware.Gzip(), middleware.Gzip(),
) )
@ -175,6 +181,12 @@ func installerRedirect(app core.App) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc { return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
// skip redirect checks for non-root level index.html requests
path := c.Request().URL.Path
if path != trailedAdminPath && path != trailedAdminPath+"index.html" {
return next(c)
}
// load into cache (if not already) // load into cache (if not already)
if !app.Cache().Has(totalAdminsCacheKey) { if !app.Cache().Has(totalAdminsCacheKey) {
if err := updateTotalAdminsCache(app); err != nil { if err := updateTotalAdminsCache(app); err != nil {
@ -188,12 +200,12 @@ func installerRedirect(app core.App) echo.MiddlewareFunc {
if totalAdmins == 0 && !hasInstallerParam { if totalAdmins == 0 && !hasInstallerParam {
// redirect to the installer page // redirect to the installer page
return c.Redirect(http.StatusTemporaryRedirect, "/_/?installer#") return c.Redirect(http.StatusTemporaryRedirect, trailedAdminPath+"?installer#")
} }
if totalAdmins != 0 && hasInstallerParam { if totalAdmins != 0 && hasInstallerParam {
// redirect to the home page // redirect to the home page
return c.Redirect(http.StatusTemporaryRedirect, "/_/#/") return c.Redirect(http.StatusTemporaryRedirect, trailedAdminPath+"#/")
} }
return next(c) return next(c)