Sha256: b8e57f5abb5f9858218526cc3ca15864757ad1166afe2eb279d462902ce9dd0d

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require "yaml"
require "erb"
require "singleton"
require "nats/client"

module Ikibana
  class ICache
    def write(...) = raise NotImplementedError, "Subclasses must implement a `write` method"
    def read(...) = raise NotImplementedError, "Subclasses must implement a `read` method"
  end

  # Configuration class for NATS
  class Config
    include Singleton

    attr_reader :config, :connection_string, :nats, :js
    attr_accessor :logger, :consumers, :cache

    def initialize(config_file = "config/nats.yaml")
      @config = load_config(config_file)
      @connection_string = @config["connection"]["url"]
      @logger = Logger.new($stdout)
      connect
      create_streams
    rescue NATS::IO::Timeout
      @logger.error("NATS server not responding")
    rescue StandardError => e
      @logger.error("#{e.class}: Error connecting to NATS server: #{e.message}")
    end

    def self.configure(config_file = "config/nats.yaml")
      yield instance if block_given?
    end

    private

    def create_streams
      @config["streams"].each do |stream|
        @js.add_stream(**stream.transform_keys(&:to_sym))
      end
    end

    def connect
      @nats ||= NATS.connect(@connection_string)
      @js ||= @nats.jetstream
      @logger.debug("Connected to NATS server at #{nats_host}")
    end

    def nats_host
      @nats_host ||= URI.parse(@connection_string).host
    end

    def load_config(file)
      raise "Configuration file #{file} not found" unless File.exist?(file)

      erb = ERB.new(File.read(file))
      YAML.safe_load(erb.result, aliases: true)
    rescue Psych::SyntaxError => e
      raise "YAML syntax error occurred while parsing #{file}: #{e.message}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ikibana-0.1.1 lib/ikibana/config.rb
ikibana-0.1.0 lib/ikibana/config.rb