Log In
Get
Docs
Showcase
Community

main.go

main.go is your app’s entry point. It initializes PocketBase, loads environment variables, sets up templates and middleware, registers routes and optional packages, and starts the server.

Use it to wire everything together. Start with a minimal setup and add features as needed.

Here’s an example from a starter app including optional Stripe payments and GitHub invites (omit those lines for simpler apps like the todo example):

package main

import (
	"dssite/app"
	"os"

	"github.com/joho/godotenv"
	"github.com/mannders00/deploysolo/pkg/github"
	"github.com/mannders00/deploysolo/pkg/middleware"
	"github.com/mannders00/deploysolo/pkg/payment"
	"github.com/mannders00/deploysolo/pkg/render"
	"github.com/pocketbase/pocketbase"
	"github.com/pocketbase/pocketbase/apis"
	"github.com/pocketbase/pocketbase/core"
)

func init() {
	err := godotenv.Load()
	if err != nil {
		panic(err)
	}
}

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

	app.LoadConfig(pb)
	render.LoadTemplates(pb, "web/templates")

	pb.OnServe().BindFunc(func(se *core.ServeEvent) error {
		se.Router.Bind(middleware.AuthCookieMiddleware())

		app.RegisterPages(se)
		app.RegisterActiveSearch(se)

		payment.RegisterPayment(se, &payment.Config{
			StripeSecretKey:     os.Getenv("STRIPE_SECRET_KEY"),
			StripeWebhookSecret: os.Getenv("STRIPE_WEBHOOK_SECRET"),
			StripePriceID:       os.Getenv("STRIPE_PRICE_ID"),
			DomainName:          os.Getenv("DOMAIN_NAME"),
			LoginEndpoint:       "/login",
		})

		github.RegisterInvite(se, &github.Config{
			GHUsername: os.Getenv("GH_USERNAME"),
			GHRepo:     os.Getenv("GH_REPO"),
			GHPAT:      os.Getenv("GH_PAT"),
		})

		se.Router.GET("/docs/{doc...}", render.RenderDocViewHandler("web/docs"))
		se.Router.GET("/public/{path...}", apis.Static(os.DirFS("web/public"), false))

		return se.Next()
	})

	if err := pb.Start(); err != nil {
		panic(err)
	}
}

Key Parts

Imports: Pull in godotenv for .env loading, DeploySolo packages (middleware, render, etc.), and PocketBase.

init(): Loads .env file for config like Stripe keys.

main(): Create PocketBase instance. Load app config and templates. Use OnServe hook to add auth middleware, register your app’s routes (e.g., from app/site.go, app/todo.go). Optionally register payment or github packages with config from env. Add static file serving for /public/.

Start the server.

Customize by adding your feature registrations (e.g., app.RegisterTodo(se)) inside the OnServe hook. See Core Packages for package details and Adding Stripe for payment setup. For production, compile to a binary instead of go run ..