From 0c74967d5db0d6af89212f3c4a35c38290cf2975 Mon Sep 17 00:00:00 2001 From: elijah Date: Sun, 12 May 2013 23:36:52 -0700 Subject: added config file --- README.md | 5 +++++ config/default.yml | 5 +++++ lib/nickserver/config.rb | 47 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 config/default.yml diff --git a/README.md b/README.md index 4b4620e..54da24c 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,11 @@ Install for development: $ bundle $ rake test +Configuration +================================== + +Nickserver loads the configuration files `config/default.yml` and `/etc/leap/nickserver.yml`, if it exists. See `config/default.yml` for the available options. + Usage ================================== diff --git a/config/default.yml b/config/default.yml new file mode 100644 index 0000000..4110b48 --- /dev/null +++ b/config/default.yml @@ -0,0 +1,5 @@ +couch_host: 'localhost' +couch_port: 5984 +couch_database: 'users' +sks_url: 'https://hkps.pool.sks-keyservers.net:/pks/lookup' +port: 6425 diff --git a/lib/nickserver/config.rb b/lib/nickserver/config.rb index d47fc68..56478e2 100644 --- a/lib/nickserver/config.rb +++ b/lib/nickserver/config.rb @@ -1,14 +1,49 @@ +require 'yaml' + module Nickserver class Config + PATHS = [ + File.expand_path('../../../config/default.yml', __FILE__), + '/etc/leap/nickserver.yml' + ] + class << self attr_accessor :sks_url + attr_accessor :couch_port + attr_accessor :couch_host + attr_accessor :couch_database attr_accessor :port + attr_accessor :loaded end - end - # - # set reasonable defaults - # - Config.sks_url = 'https://hkps.pool.sks-keyservers.net:/pks/lookup' - Config.port = 6425 # aka "NICK" + def self.load + self.loaded ||= begin + PATHS.each do |file_path| + self.load_config(file_path) + end + true + end + end + + private + + def self.load_config(file_path) + begin + YAML.load(File.read(file_path)).each do |key, value| + begin + self.send("#{key}=", value) + rescue NoMethodError => exc + STDERR.puts "ERROR in file #{file_path}, '#{key}' is not a valid option" + exit(1) + end + end + puts "Loaded #{file_path}" + rescue Errno::ENOENT => exc + puts "Skipping #{file_path}" + rescue Exception => exc + STDERR.puts exc.inspect + exit(1) + end + end + end end -- cgit v1.2.3