Overview
pb-ext uses the functional options pattern for server configuration. This provides a flexible, type-safe way to customize server behavior without breaking changes.Server Options
All server options are defined incore/server/server_options.go and re-exported through the public facade.
Available Options
WithConfig
Provide a custom PocketBase configuration
WithPocketbase
Use an existing PocketBase instance
InDeveloperMode
Enable developer mode (hot reload, verbose logging)
InNormalMode
Enable production mode (optimized, minimal logging)
Option Functions
InDeveloperMode / InNormalMode
The simplest way to configure the server:cmd/server/main.go
core/server/server_options.go
Developer mode sets PocketBase’s
DefaultDev flag, enabling features like auto-migrations and verbose logging.WithConfig
Provide a custom PocketBase configuration:core/server/server_options.go
WithPocketbase
Use an existing PocketBase instance:core/server/server_options.go
WithMode
Generic mode setter (used internally byInDeveloperMode / InNormalMode):
core/server/server_options.go
Configuration Patterns
Pattern 1: Simple Mode Toggle
The most common pattern - toggle between dev and production:cmd/server/main.go
Pattern 2: Custom PocketBase Config
Provide a custom data directory or other PocketBase settings:Pattern 3: Existing PocketBase Instance
Use an existing PocketBase instance (useful for testing or advanced setups):Pattern 4: Custom Port
Set a custom port using command-line arguments:cmd/server/main.go
PocketBase reads port configuration from
os.Args. Set it before calling srv.Start().Environment Variables
While pb-ext doesn’t enforce specific environment variables, you can integrate them with standard Go patterns:Common Environment Variables
PocketBase Integration
Accessing PocketBase
The underlying PocketBase instance is accessible viasrv.App():
PocketBase Configuration
Thepocketbase.Config struct supports these fields:
Hook Registration
Register hooks on the PocketBase instance before starting the server:Complete Example
Here’s the complete example fromcmd/server/main.go:
cmd/server/main.go
Options Internal Structure
The internaloptions struct:
core/server/server_options.go
Configuration Conflict
Attempting to use bothWithConfig and WithPocketbase results in a panic:
core/server/server_options.go
Best Practices
1
Use Simple Mode Toggle
Prefer
InDeveloperMode() / InNormalMode() for most use cases2
Only Customize When Needed
Only use
WithConfig / WithPocketbase if you need advanced PocketBase customization3
Environment-Aware Configuration
Read mode and settings from environment variables or flags
4
Register Hooks Before Start
Always register user hooks before calling
srv.Start()Next Steps
Server Lifecycle
Understand bootstrap, serve, and runtime phases
Job Management
Learn how to register and manage cron jobs