mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-04-24 08:33:02 +02:00
[#470] added --pbPublic flag
This commit is contained in:
parent
ed5f3b86f5
commit
1f5c3328e5
@ -2,21 +2,40 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/labstack/echo/v5"
|
|
||||||
"github.com/pocketbase/pocketbase"
|
"github.com/pocketbase/pocketbase"
|
||||||
"github.com/pocketbase/pocketbase/apis"
|
"github.com/pocketbase/pocketbase/apis"
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"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() {
|
func main() {
|
||||||
app := pocketbase.New()
|
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 {
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||||
// serves static files from the provided public dir (if exists)
|
// 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(os.DirFS(publicDirFlag), false))
|
||||||
e.Router.GET("/*", apis.StaticDirectoryHandler(subFs, false))
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user