Custom Application

You can still take advantage of the PocketBuilds ecosystem even when building a custom pocketbase application. In fact, using xpb in this way is a great way to organize and modularize your project.

my_project/
 hooks/
 hooks.go
 api/
 api.go
 go.mod
 go.sum
 main.go
// main.go
module my_project

import (
	// xpb and default, built-in plugins
	"github.com/pocketbuilds/xpb"
	_ "github.com/pocketbuilds/xpb/pkg/plugins/defaults"

	// project plugins
	_ "my_project/hooks"
	_ "my_project/api"

	// external plugins
	_ "github.com/pocketbuilds/created_by"
	_ "github.com/pocketbuilds/last_login"
)

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

	if err := xpb.Setup(app); err != nil {
		log.Fatal(err)
	}

	if err := app.Start(); err != nil {
		log.Fatal(err)
	}
}
// api.go
module api

type Plugin struct {
	MyApiPrefix string `json:"my_api_prefix"`
}

func init() {
	xpb.Register(&Plugin{
		MyApiPrefix: "my-api",
	})
}

// Name implements xpb.Plugin.
func (p *Plugin) Name() string {
	return "my_project_api" 
}
// Version implements xpb.Plugin.
func (p *Plugin) Version() string {
	return ""
}

func (p *Plugin) Description() string {
	return ""
}

func (p *Plugin) Init(app core.App) error {
	app.OnServe().BindFunc(func(se *core.ServeEvent) error {
		var group = e.Router.Group(p.MyApiPrefix)
		group.GET("/hello/{name}", func(re *core.RequestEvent) error {
			name := e.Request.PathValue("name")

			return e.String(http.StatusOK, "Hello " + name)
		})
		return e.Next()
	})
	return nil
}