Class: Boat::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/boat/server.rb

Defined Under Namespace

Modules: BoatServer

Constant Summary collapse

ConfigurationError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/boat/server.rb', line 8

def configuration
  @configuration
end

Instance Method Details

#load_configurationObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/boat/server.rb', line 238

def load_configuration
  unless File.exists?(@config_file)
    raise "configuration file #{config_file} does not exist"
  end

  configuration = YAML.load(IO.read(@config_file))
  if configuration["users"].nil? || configuration["users"].empty?
    raise "configuration file does not have any users defined in it"
  end

  configuration["storage_path"] ||= Boat::DEFAULT_STORAGE_DIRECTORY
  FileUtils.mkdir_p("#{configuration["storage_path"]}/tmp")

  @configuration.update(configuration)
rescue => e
  raise ConfigurationError, e.message, $@
end

#runObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/boat/server.rb', line 256

def run
  trap('SIGINT') { exit }
  trap('SIGTERM') { exit }

  while ARGV.first && ARGV.first[0..0] == '-' && ARGV.first.length > 1
    case opt = ARGV.shift
    when '-c' then @config_file = ARGV.shift
    else           raise "unknown commandline option #{opt}"
    end
  end

  @config_file ||= Boat::DEFAULT_SERVER_CONFIGURATION_FILE
  @configuration = {}
  load_configuration

  trap('SIGHUP') do
    begin
      load_configuration
    rescue ConfigurationError => e
      STDERR.puts "Could not reload configuration file: #{e.message}"
    end
  end

  File.umask(0077)
  EventMachine.run do
    EventMachine.start_server(
      @configuration.fetch("listen_address", "localhost"),
      @configuration.fetch("listen_port", Boat::DEFAULT_PORT),
      BoatServer,
      @configuration)
  end
end