Skip to main content

Overview

The Server type is the main entry point for pb-ext applications. It wraps a PocketBase instance and adds production-ready features including analytics, job management, health monitoring, and structured logging.

Type Definition

Location: core/server/server.go:18

Constructor

New

Creates a new Server instance with optional configuration.
Parameters:
...Option
Variadic functional options for configuring the server. See Options for available options.
Returns: *Server - A configured server instance ready to start. Example:
Location: core/server/server.go:43
The constructor initializes the server but does not start it. Call Start() to begin serving requests.

Methods

Start

Initializes and starts the server, including all subsystems (analytics, jobs, health monitoring).
Returns: error - Returns an error if the server fails to start, nil otherwise. Lifecycle:
  1. Bootstrap Phase (OnBootstrap):
    • Initializes job management system
    • Registers internal system jobs
    • Sets up job handlers
  2. Serve Phase (OnServe):
    • Registers request tracking middleware
    • Initializes analytics system
    • Registers job API routes
    • Serves static files from pb_public/
    • Starts the HTTP server
Example:
Location: core/server/server.go:80
Start() is a blocking call. It will run until the server is stopped or encounters a fatal error.

App

Returns the underlying PocketBase instance for direct access to PocketBase APIs.
Returns: *pocketbase.PocketBase - The wrapped PocketBase instance. Example:
Location: core/server/server.go:260

Stats

Returns the current server statistics.
Returns: *ServerStats - Server statistics including request counts, active connections, and timing data. Example:
Location: core/server/server.go:265

ServerStats Type

Tracks real-time server metrics using atomic operations for thread-safe access.
Location: core/server/server.go:28

Fields

time.Time
Server startup timestamp (not atomic, set once at initialization).
atomic.Uint64
Total number of HTTP requests processed. Excludes /service-worker.js, /favicon.ico, and /manifest.json.Access: stats.TotalRequests.Load()
atomic.Int32
Current number of active HTTP connections being processed.Access: stats.ActiveConnections.Load()
atomic.Int64
Unix timestamp (seconds) of the last processed request.Access: time.Unix(stats.LastRequestTime.Load(), 0)
atomic.Uint64
Total number of requests that returned errors.Access: stats.TotalErrors.Load()
atomic.Int64
Rolling average request processing time in nanoseconds.Access: time.Duration(stats.AverageRequestTime.Load())
All numeric fields use atomic types for thread-safe concurrent access. Use the .Load() method to read values.

Complete Example

See Also