Skip to main content

Builder Pattern

The SDK uses a fluent builder pattern for constructing service instances. This provides type-safe configuration with clear validation.

Basic Builder

Required Configuration

Two fields are required:
If either is missing, Build() returns an error:

Builder Options

Configuration Methods

WithConfig

Sets the configuration instance:

WithConfigPath

Sets the file path for configuration watching:
When the file changes, the configuration is automatically reloaded.

Authentication Providers

WithTokenClientProvider

Customize how token-backed clients are loaded:

WithAPIKeyClientProvider

Customize API key client loading:

Authentication Managers

WithAuthManager

Override the legacy authentication manager:

WithCoreAuthManager

Customize the core authentication manager:

WithRequestAccessManager

Set the request access manager for authentication:

File Watching

WithWatcherFactory

Customize configuration and auth file watching:

Server Options

WithServerOptions

Add HTTP server configuration options:

WithLocalManagementPassword

Set a password for localhost management endpoints:
This restricts management API access:
Management endpoints are only accessible from localhost (127.0.0.1) for security.

WithPostAuthHook

Register a hook called after auth creation but before persistence:

Lifecycle Hooks

Hooks allow you to execute custom code during service lifecycle events.

OnBeforeStart

Called before the service starts, allowing configuration modifications:

OnAfterStart

Called after successful startup, providing access to the running service:

Complete Hooks Example

Complete Builder Example

Here’s a comprehensive example using multiple builder options:

Service Management

Starting the Service

The Run method:
  • Starts the HTTP server
  • Initializes authentication
  • Starts file watchers
  • Blocks until context is cancelled

Stopping the Service

The Shutdown method:
  • Stops accepting new requests
  • Completes in-flight requests
  • Stops file watchers
  • Cleans up resources
  • Is idempotent (safe to call multiple times)

Next Steps

Advanced Features

Learn about custom executors and translators

Access Providers

Implement custom authentication

File Watching

Understand config and auth file watching

Usage Tracking

Monitor API usage and consumption