mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-02-13 00:16:19 +02:00
[#470] added --pbPublic flag
This commit is contained in:
parent
ed5f3b86f5
commit
1f5c3328e5
@ -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
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user