Foundation.Services.ConfigServer (foundation v0.1.0)
GenServer implementation for configuration management.
Handles configuration persistence, updates, and notifications. Delegates business logic to ConfigLogic module.
This server provides a centralized point for configuration access and modification, with support for subscriptions to configuration changes.
See @type server_state
for the internal state structure.
Examples
# Get configuration
{:ok, config} = Foundation.Services.ConfigServer.get()
# Update a configuration value
:ok = Foundation.Services.ConfigServer.update([:ai, :provider], :openai)
# Subscribe to configuration changes
:ok = Foundation.Services.ConfigServer.subscribe()
Summary
Functions
Check if the configuration service is available.
Returns a specification to start this module under a supervisor.
Get the complete configuration.
Get a configuration value by path.
Initialize the GenServer state.
Initialize the configuration service with default options.
Initialize the configuration service with custom options.
Reset configuration to defaults.
Reset all internal state for testing purposes.
Start the configuration server.
Get configuration service status.
Stop the configuration server.
Subscribe to configuration change notifications.
Unsubscribe from configuration change notifications.
Get the list of paths that can be updated at runtime.
Update a configuration value at the given path.
Validate a configuration structure.
Types
@type metrics() :: %{ start_time: integer(), updates_count: non_neg_integer(), last_update: integer() | nil }
Metrics tracking for the configuration server
@type server_state() :: %{ config: Foundation.Types.Config.t(), subscribers: [pid()], monitors: %{required(reference()) => pid()}, metrics: metrics(), namespace: Foundation.ProcessRegistry.namespace() }
Internal state of the configuration server
Functions
@spec available?() :: boolean()
Check if the configuration service is available.
Returns true if the GenServer is running and registered.
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec get() :: {:ok, Foundation.Types.Config.t()} | {:error, Foundation.Types.Error.t()}
Get the complete configuration.
Returns the current configuration or an error if the service is unavailable.
@spec get([atom()]) :: {:ok, term()} | {:error, Foundation.Types.Error.t()}
Get a configuration value by path.
Parameters
path
: List of atoms representing the path to the configuration value
Examples
{:ok, provider} = get([:ai, :provider])
{:ok, timeout} = get([:capture, :processing, :timeout])
@spec init(keyword()) :: {:ok, server_state()} | {:stop, term()}
Initialize the GenServer state.
Builds the initial configuration and sets up metrics tracking.
@spec initialize() :: :ok | {:error, Foundation.Types.Error.t()}
Initialize the configuration service with default options.
Examples
:ok = Foundation.Services.ConfigServer.initialize()
@spec initialize(keyword()) :: :ok | {:error, Foundation.Types.Error.t()}
Initialize the configuration service with custom options.
Parameters
opts
: Keyword list of initialization options
Examples
:ok = Foundation.Services.ConfigServer.initialize(cache_size: 1000)
@spec reset() :: :ok | {:error, Foundation.Types.Error.t()}
Reset configuration to defaults.
Resets the configuration to its default values and notifies all subscribers.
@spec reset_state() :: :ok | {:error, Foundation.Types.Error.t()}
Reset all internal state for testing purposes.
Clears all subscribers, metrics, and resets configuration to defaults. This function should only be used in test environments.
@spec start_link(keyword()) :: GenServer.on_start()
Start the configuration server.
Parameters
opts
: Keyword list of options passed to GenServer initialization:namespace
- The namespace to register in (defaults to :production)
@spec status() :: {:ok, map()} | {:error, Foundation.Types.Error.t()}
Get configuration service status.
Examples
{:ok, status} = Foundation.Services.ConfigServer.status()
@spec stop() :: :ok
Stop the configuration server.
@spec subscribe(pid()) :: :ok | {:error, Foundation.Types.Error.t()}
Subscribe to configuration change notifications.
Parameters
pid
: Process to subscribe (defaults to calling process)
Examples
:ok = Foundation.Services.ConfigServer.subscribe()
:ok = Foundation.Services.ConfigServer.subscribe(some_pid)
@spec unsubscribe(pid()) :: :ok | {:error, Foundation.Types.Error.t()}
Unsubscribe from configuration change notifications.
Parameters
pid
: Process to unsubscribe (defaults to calling process)
@spec updatable_paths() :: [[atom(), ...], ...]
Get the list of paths that can be updated at runtime.
Delegates to the ConfigLogic module for the list of updatable paths.
@spec update([atom()], term()) :: :ok | {:error, Foundation.Types.Error.t()}
Update a configuration value at the given path.
Parameters
path
: List of atoms representing the path to the configuration valuevalue
: New value to set
Examples
:ok = update([:ai, :provider], :openai)
:ok = update([:capture, :ring_buffer, :size], 2048)
@spec validate(Foundation.Types.Config.t()) :: :ok | {:error, Foundation.Types.Error.t()}
Validate a configuration structure.
Delegates to the ConfigValidator module for validation logic.