config

package module
v0.0.39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 12 Imported by: 3

README

Station Manager: config package

Service responsible for loading and exposing application configuration.

Generated config.json

On first run, if no config.json is present in the working directory, the service writes a default, pretty-printed file with restrictive permissions (0640).

Selecting default datastore via environment

You can choose which datastore block gets written into a newly generated config.json using the environment variable SM_DEFAULT_DB.

Accepted values (case-insensitive):

  • sqlite (default)
  • postgres, postgresql, pg

Example:

# Use PostgreSQL defaults when generating a fresh config.json
export SM_DEFAULT_DB=pg

If the file already exists, the variable is ignored and the existing config is loaded.

Working directory resolution

The config service resolves its working directory via:

  1. The injected WorkingDir field if provided
  2. The SM_WORKING_DIR env var
  3. The directory of the running executable

The resolved directory must exist.

Programmatic overrides (advanced)

If you pre-seed Service.AppConfig.LoggingConfig before calling Initialize(), the config service preserves that logging section instead of the one loaded from config.json. This is intentional to support:

  • Unit/integration tests that need to inject invalid levels or specific writer options
  • Programmatic bootstraps that want to override logging without modifying files

Notes:

  • The sentinel for preserving is a non-empty LoggingConfig.Level.
  • Only the logging section is preserved; other sections (e.g., datastore) still load from file.
  • For typical usage, leave AppConfig empty and rely on the file.

Defaults and tuning guidance

The defaults aim for sensible behavior out of the box and should be tuned per environment and workload.

SQLite (modernc.org/sqlite)
  • MaxOpenConns: 4 (readers benefit; writes are serialized)
  • MaxIdleConns: 4
  • ConnMaxLifetime: 0 (no forced recycle)
  • ConnMaxIdleTime: 5m
  • ContextTimeout: 5s
  • TransactionContextTimeout: 10s
  • Options:
    • _journal_mode=WAL
    • _busy_timeout=5000 (consider 10000 under heavier write contention)
    • _foreign_keys=on

Notes:

  • WAL enables concurrent readers. Too many connections can increase lock contention; start small and measure.
  • Transaction timeout should be longer than typical busy periods to avoid spurious cancellations.
PostgreSQL
  • MaxOpenConns: 10 (consider 20+ for higher concurrency)
  • MaxIdleConns: 5–10
  • ConnMaxLifetime: 30–60m (keep below server rotation)
  • ConnMaxIdleTime: 5–10m
  • ContextTimeout: 5–10s
  • TransactionContextTimeout: 15–30s
  • SSLMode: disable for local dev; use require/verify-ca/verify-full in production

Notes:

  • Tune pool sizes based on CPU, workload mix, and DB server capacity.
  • Longer lifetimes reduce churn; align with server-side settings.

API

  • Initialize() error — loads (or creates) config.json in the working directory; idempotent.
  • DatastoreConfig() (types.DatastoreConfig, error) — returns the datastore settings.
  • LoggingConfig() (types.LoggingConfig, error) — returns the logging settings.

Downstream services (database, logging) validate their respective sections when they initialize.

Documentation

Index

Constants

View Source
const (
	ServiceName = types.ConfigServiceName

	// EnvSmDefaultDB selects the default datastore driver when generating a new config.json.
	// Accepts: "sqlite" (default), "postgres", and common aliases like "postgresql" or "pg".
	EnvSmDefaultDB = "SM_DEFAULT_DB"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

type Service struct {
	WorkingDir string `di.inject:"workingdir"`
	AppConfig  types.AppConfig
	// contains filtered or unexported fields
}

func (*Service) CatStateValues added in v0.0.14

func (s *Service) CatStateValues() (types.StateValues, error)

CatStateValues retrieves the CAT state values for the default rig configuration in the service's application configuration. Returns a map of state values organized by tags or an error if the service is uninitialized or fails to retrieve the configuration.

func (*Service) DatastoreConfig added in v0.0.4

func (s *Service) DatastoreConfig() (types.DatastoreConfig, error)

DatastoreConfig returns the datastore configuration.

func (*Service) EmailConfig added in v0.0.27

func (s *Service) EmailConfig() (types.EmailConfig, error)

EmailConfig retrieves the email configuration from the application configuration. Returns an error if uninitialized.

func (*Service) ForwarderConfig added in v0.0.23

func (s *Service) ForwarderConfig(serviceName string) (types.ForwarderConfig, error)

ForwarderConfig retrieves the forwarder configuration for the specified service name. Returns a ForwarderConfig object and nil error if found, otherwise returns an empty object and an appropriate error.

func (*Service) ForwarderConfigs added in v0.0.31

func (s *Service) ForwarderConfigs() ([]types.ForwarderConfig, error)

func (*Service) Initialize

func (s *Service) Initialize() error

Initialize initializes the config service.

func (*Service) LoggingConfig

func (s *Service) LoggingConfig() (types.LoggingConfig, error)

LoggingConfig returns the logging configuration.

func (*Service) LoggingStationConfigs added in v0.0.17

func (s *Service) LoggingStationConfigs() (types.LoggingStation, error)

LoggingStationConfigs retrieves the logging station configuration from the service's application configuration.

func (*Service) LookupServiceConfig added in v0.0.17

func (s *Service) LookupServiceConfig(serviceName string) (types.LookupConfig, error)

LookupServiceConfig fetches the configuration for a given service by its name from the loaded application settings.

func (*Service) RequiredConfigs added in v0.0.9

func (s *Service) RequiredConfigs() (types.RequiredConfigs, error)

RequiredConfigs retrieves the required configurations for the application. Returns an error if the service is uninitialized.

func (*Service) RigConfigByID added in v0.0.9

func (s *Service) RigConfigByID(rigID int64) (types.RigConfig, error)

RigConfigByID retrieves the RigConfig for the given rig ID from the service's AppConfig. Returns an error if unavailable.

func (*Service) ServerConfig added in v0.0.8

func (s *Service) ServerConfig() (*types.ServerConfig, error)

ServerConfig returns the server configuration from the application configuration. It requires the service to be initialized.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL