Class: Hooks::Core::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/hooks/core/config_loader.rb

Overview

Loads and merges configuration from files and environment variables

Constant Summary collapse

DEFAULT_CONFIG =
{
  handler_plugin_dir: "./plugins/handlers",
  auth_plugin_dir: "./plugins/auth",
  log_level: "info",
  request_limit: 1_048_576,
  request_timeout: 30,
  root_path: "/webhooks",
  health_path: "/health",
  version_path: "/version",
  environment: ENV.fetch("RACK_ENV", "production"),
  endpoints_dir: "./config/endpoints",
  use_catchall_route: false,
  normalize_headers: true,
  default_format: :json
}.freeze
SILENCE_CONFIG_LOADER_MESSAGES =
ENV.fetch(
  "HOOKS_SILENCE_CONFIG_LOADER_MESSAGES", "false"
).downcase == "true".freeze

Class Method Summary collapse

Class Method Details

.load(config_path: nil) ⇒ Hash

Load and merge configuration from various sources

Parameters:

  • config_path (String, Hash) (defaults to: nil)

    Path to config file or config hash

Returns:

  • (Hash)

    Merged configuration

Raises:

  • (ArgumentError)

    if config file path is provided but file doesn’t exist

  • (RuntimeError)

    if config file exists but fails to load



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hooks/core/config_loader.rb', line 36

def self.load(config_path: nil)
  config = DEFAULT_CONFIG.dup
  overrides = []

  # Load from file if path provided
  if config_path.is_a?(String)
    unless File.exist?(config_path)
      raise ArgumentError, "Configuration file not found: #{config_path}"
    end

    file_config = load_config_file(config_path)
    if file_config
      overrides << "file config"
      config.merge!(file_config)
    else
      raise RuntimeError, "Failed to load configuration from file: #{config_path}"
    end
  end

  # Override with environment variables (before programmatic config)
  env_config = load_env_config
  if env_config.any?
    overrides << "environment variables"
    config.merge!(env_config)
  end

  # Programmatic config has highest priority
  if config_path.is_a?(Hash)
    overrides << "programmatic config"
    config.merge!(config_path)
  end

  # Convert string keys to symbols for consistency
  config = symbolize_keys(config)

  # Log overrides if any were made
  if overrides.any?
    puts "INFO: Configuration overrides applied from: #{overrides.join(', ')}" unless SILENCE_CONFIG_LOADER_MESSAGES
  end

  return config
end

.load_endpoints(endpoints_dir) ⇒ Array<Hash>

Load endpoint configurations from directory

Parameters:

  • endpoints_dir (String)

    Directory containing endpoint config files

Returns:

  • (Array<Hash>)

    Array of endpoint configurations



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hooks/core/config_loader.rb', line 83

def self.load_endpoints(endpoints_dir)
  return [] unless endpoints_dir && Dir.exist?(endpoints_dir)

  endpoints = []
  files = Dir.glob(File.join(endpoints_dir, "*.{yml,yaml,json}"))

  files.each do |file|
    endpoint_config = load_config_file(file)
    if endpoint_config
      endpoints << symbolize_keys(endpoint_config)
    end
  end

  endpoints
end