Skip to content

Commit

Permalink
[pocketbase#470] added --pbPublic flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Sep 16, 2022
1 parent ed5f3b8 commit 1f5c332
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions examples/base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,40 @@ package main

import (
"log"
"os"
"path/filepath"
"strings"

"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
)

func defaultPublicDir() string {
if strings.HasPrefix(os.Args[0], os.TempDir()) {
// most likely ran with go run
return "./pb_public"
}

return filepath.Join(os.Args[0], "../pb_public")
}

func main() {
app := pocketbase.New()

var publicDirFlag string

// add "--publicDir" option flag
app.RootCmd.PersistentFlags().StringVar(
&publicDirFlag,
"publicDir",
defaultPublicDir(),
"the directory to serve static files",
)

app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
// serves static files from the provided public dir (if exists)
subFs := echo.MustSubFS(e.Router.Filesystem, filepath.Join(app.DataDir(), "../pb_public"))
e.Router.GET("/*", apis.StaticDirectoryHandler(subFs, false))
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS(publicDirFlag), false))

return nil
})
Expand Down

0 comments on commit 1f5c332

Please sign in to comment.