71 lines
1.9 KiB
Ruby
71 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'psych'
|
|
|
|
require_relative 'util/cache'
|
|
require_relative 'util/database'
|
|
require_relative 'util/hash_extensions'
|
|
require_relative 'util/time_extensions'
|
|
|
|
module FicTracker::Config
|
|
class << self
|
|
def load!(config_file: 'config.yml')
|
|
@config_file = config_file
|
|
@config = {}
|
|
|
|
load_internal
|
|
|
|
FicTracker.database = FicTracker::Util::Database.connect(
|
|
dig(:database, :url, default: :memory),
|
|
migrate: dig(:database, :migrate, default: true),
|
|
options: dig(:database, :options, default: {})
|
|
)
|
|
FicTracker.cache = FicTracker::Util::Cache.create(
|
|
type: dig(:cache, :type, default: :none),
|
|
compress: dig(:cache, :compress, default: nil),
|
|
encoding: dig(:cache, :encoding, default: nil),
|
|
options: dig(:cache, :options, default: {})
|
|
)
|
|
FicTracker.logger.level = dig(:log, :level, default: :info)
|
|
|
|
Sequel::Model.db = FicTracker.database
|
|
end
|
|
|
|
def [](arg)
|
|
@config[arg]
|
|
end
|
|
|
|
def dig(*args, default: nil)
|
|
envvar = (%i[ft] + args.map { |arg| arg.to_s }).join('_').upcase
|
|
return ENV[envvar] if ENV[envvar]
|
|
|
|
ret = @config.dig(*args)
|
|
return ret if ret
|
|
return default unless default.nil?
|
|
return yield if block_given?
|
|
|
|
nil
|
|
end
|
|
|
|
private
|
|
|
|
def load_internal
|
|
@config_file = ENV['FT_CONFIG_FILE'] if ENV['FT_CONFIG_FILE']
|
|
begin
|
|
# puts "Loading config #{@config_file}"
|
|
@config = Psych.load(File.read(@config_file)).deep_transform_keys(&:to_sym)
|
|
rescue StandardError => e
|
|
puts "Failed to load config #{@config_file}, #{e.class}: #{e}"
|
|
@config = {}
|
|
end
|
|
@config[:database] ||= {}
|
|
@config[:database][:config] ||= {}
|
|
@config[:cache] ||= {}
|
|
|
|
if ENV['FT_CACHE_REDIS_URL']
|
|
@config[:cache][:type] = :redis
|
|
@config[:cache][:url] = ENV['FT_CACHE_REDIS_URL']
|
|
end
|
|
end
|
|
end
|
|
end
|