Automatically creating collections and admin account on first run #5481
-
|
Hi,
on this page https://pocketbase.io/docs/go-migrations but when I tried to run can someone help with this? Also, I want to create a default admin account :) here's my package main
import (
"embed"
"errors"
"fmt"
"io/fs"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
)
//go:embed all:build
var distDir embed.FS
// DistDirFS contains the embedded dist directory files (without the "dist" prefix)
var appBuildDirFs = echo.MustSubFS(distDir, "build")
func main() {
app := pocketbase.New()
// serves static files from the provided public dir (if exists)
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public"), false))
e.Router.GET(
"/*",
StaticDirectoryHandler(appBuildDirFs, true, "404.html"),
middleware.Gzip(),
)
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
func StaticDirectoryHandler(fileSystem fs.FS, indexFallback bool, fallbackPage ...string) echo.HandlerFunc {
return func(c echo.Context) error {
p := c.PathParam("*")
// escape url path
tmpPath, err := url.PathUnescape(p)
if err != nil {
return fmt.Errorf("failed to unescape path variable: %w", err)
}
p = tmpPath
// fs.FS.Open() already assumes that file names are relative to FS root path and considers name with prefix `/` as invalid
name := filepath.ToSlash(filepath.Clean(strings.TrimPrefix(p, "/")))
fileErr := c.FileFS(name, fileSystem)
// Determine the fallback page
fallback := "404.html"
if len(fallbackPage) > 0 {
fallback = fallbackPage[0]
}
if fileErr != nil && indexFallback && errors.Is(fileErr, echo.ErrNotFound) {
return c.FileFS(fallback, fileSystem)
}
return fileErr
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
I'm sorry but this is just lazy and low effort. Please read the documentation that you've linked (https://pocketbase.io/docs/go-migrations). Everything is explained there. If something is not clear, elaborate on what part you didn't understand. There is even an example with migration how to create the initial admin account. |
Beta Was this translation helpful? Give feedback.
I'm sorry but this is just lazy and low effort. Please read the documentation that you've linked (https://pocketbase.io/docs/go-migrations). Everything is explained there. If something is not clear, elaborate on what part you didn't understand. There is even an example with migration how to create the initial admin account.