Skip to main content

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 in core/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
Implementation:
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:
Implementation:
core/server/server_options.go
WithConfig and WithPocketbase cannot be used together. If both are provided, the server will panic with ErrConfigurationConflict.

WithPocketbase

Use an existing PocketBase instance:
Implementation:
core/server/server_options.go
Use WithPocketbase when you need fine-grained control over the PocketBase instance before pb-ext wraps it.

WithMode

Generic mode setter (used internally by InDeveloperMode / 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 via srv.App():

PocketBase Configuration

The pocketbase.Config struct supports these fields:

Hook Registration

Register hooks on the PocketBase instance before starting the server:

Complete Example

Here’s the complete example from cmd/server/main.go:
cmd/server/main.go

Options Internal Structure

The internal options struct:
core/server/server_options.go

Configuration Conflict

Attempting to use both WithConfig and WithPocketbase results in a panic:
core/server/server_options.go

Best Practices

1

Use Simple Mode Toggle

Prefer InDeveloperMode() / InNormalMode() for most use cases
2

Only Customize When Needed

Only use WithConfig / WithPocketbase if you need advanced PocketBase customization
3

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