class Pokan::ServerConfiguration
Class used to store and manage server configuration. Should not be used by user code, just by the Pokan::Cluster
class. It loads a YAML configuration file located in the user home directory or in the .config
directory or in /etc/pokan.yml
. Optionally, a different config file can be passed.
Usage¶ ↑
s = Pokan::ServerConfiguration.new("pokan.yml") s.tcp_port #=> 8888 (depending on your configuration options)
A general configuration file has the following skeleton:
pokan: connection: udp: 88885 tcp: 7777 seed: "10.10.10.8" log: enabled: true log_file: "/var/log/pokan.log"
Constants
- YAML_ROOT
Public Class Methods
Creates a new instance of Pokan::ServerConfiguration
. You can pass an optional parameter indicating the location of the configuration file (must be a valid YAML file)
If no config file is passed, we first look if there is a .pokan.yml
file in the user’s home directory. If there isn’t, then we search for a .config/pokan/pokan.yml
. If none of these are found, then we set the default values (see Pokan::ServerConfiguration#set_defaults
for further details)
# File lib/server_configuration.rb, line 40 def initialize(config_file=nil) @config_file = config_file || search_config @config_file.nil? ? set_defaults : load_config(@config_file) end
Public Instance Methods
Returns the absolute path of the configuration file, if any.
# File lib/server_configuration.rb, line 70 def file_path @config_file end
Returns true
if a configuration file was found, or if the user explicitly passed a file.
# File lib/server_configuration.rb, line 65 def has_config_file? !@config_file.nil? end
# File lib/server_configuration.rb, line 79 def load_config(config_file) raise "YAML file not found: #{config_file}" unless File.exists? config_file @data = YAML.load_file(config_file) define_methods_for(@data[YAML_ROOT].keys) end
Returns true
if we are logging activities on the server
# File lib/server_configuration.rb, line 75 def logging? @logging end
Searches for a valid YAML configuration file in .pokan.yml
located in the user’s home directory or in .config/pokan/pokan.yml'
, or in /etc/pokan.yml
. Returns the file name, if found, or nil
if no configuration file was found.
# File lib/server_configuration.rb, line 51 def search_config home_file = File.absolute_path '~/.pokan.yml' config_file = File.absolute_path '~/.config/pokan/pokan.yml' etc_file = '/etc/pokan.yml' [home_file, config_file, etc_file].each do |config| return config if File.exists? config end nil end
Sets the default values for all the attributes that can be set with the configuration file. These are:
-
TCP port: 5555
-
UDP port: 5555
-
no seeds information
-
logger enabled
-
log_file ./pokan.log
# File lib/server_configuration.rb, line 93 def set_defaults @tcp_port = 5555 @udp_port = 5555 @seeds = [] @logging = true @log_file = './pokan.log' end
Private Instance Methods
Defines methods in the singleton class for every key in the keys
parameter
# File lib/server_configuration.rb, line 105 def define_methods_for(keys) keys.each do |k| singleton_class.send(:define_method, k) do @data[YAML_ROOT][k] end end end
# File lib/server_configuration.rb, line 113 def singleton_class class << self; self; end end