Sha256: 407fd8889b496ed2f5c2a7626d295ee01f399dde6377a3cacc0ddc68c925c1e3

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

require 'optparse'
require 'tmpdir'

logger = nil

if Object.const_defined? :Logger
  logger = Logger.new(STDERR)
  logger = Logger.new(STDOUT)
end

ServerConfigDefaults = {
  'port' => 6789,
  'ip' => '0.0.0.0',
  'logger' => logger,
  'max_message_length' => 500,
  'tmp_files' => File.join(Dir::tmpdir, 'jschat'),
  'db_name' => 'jschat',
  'db_host' => 'localhost',
  'db_port' => 27017,
  #'db_user' => '',
  #'db_password' => '',
  # Register your instance of JsChat here: http://twitter.com/apps/create
  # 'twitter' => { 'key' => '', 'secret' => '' }
}

# Command line options will overrides these
def load_options(path)
  path = File.expand_path path
  if File.exists? path
    JSON.parse(File.read path)
  else
    {}
  end
end

def make_tmp_files
  ServerConfig['use_tmp_files'] = false
  if File.exists? ServerConfig['tmp_files']
    ServerConfig['use_tmp_files'] = true
  else
    if Dir.mkdir ServerConfig['tmp_files']
      ServerConfig['use_tmp_files'] = true
    end
  end
end

options = {}
default_config_file = '/etc/jschat/config.json'

ARGV.clone.options do |opts|
  script_name = File.basename($0)
  opts.banner = "Usage: #{$0} [options]" 

  opts.separator ""

  opts.on("-c", "--config=PATH", String, "Configuration file location (#{default_config_file}") { |o| options['config'] = o }
  opts.on("-p", "--port=PORT", String, "Port number") { |o| options['port'] = o }
  opts.on("-t", "--tmp_files=PATH", String, "Temporary files location (including pid file)") { |o| options['tmp_files'] = o }
  opts.on("--help", "-H", "This text") { puts opts; exit 0 }

  opts.parse!
end

options = load_options(options['config'] || default_config_file).merge options

ServerConfig = ServerConfigDefaults.merge options
make_tmp_files

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jschat-0.2.3 lib/jschat/server_options.rb
jschat-0.2.2 lib/jschat/server_options.rb
jschat-0.2.1 lib/jschat/server_options.rb