module OddJob

Constants

DEFAULT_PORT
INFO_PATH
UPLOAD_PATH
VERSION

Public Class Methods

server(opts) click to toggle source

Start the oddjob server.

opts is a hash. Allowed keys are:

  • :serverroot - directory to serve (default CWD)

  • :savedirectory - where to save uploads (default dump to STDOUT)

  • :usagemessage - the command line usage message to dispaly on info page

  • :allowall - serve to clients other than on localhost? (default false)

  • :networkdelay - simulated network delay (default no delay).

  • :port - port to use. (default is in DEFAULT_PORT module constant)

Runs the server until a TERM or INT signal is received (e.g. ctrl-c from the command line).

# File lib/oddjob.rb, line 47
def OddJob.server(opts)
  defaults = {
    :serverroot     => ".",
    :savedirectory  => nil,
    :usagemessage   => nil,
    :allowall       => false,
    :networkdelay   => 0,
    :port           => DEFAULT_PORT
  }

  options = defaults.merge(opts)

  # Add any missing MIME types (http://bugs.ruby-lang.org/issues/5365)
  m_types = WEBrick::HTTPUtils::DefaultMimeTypes.dup
  m_types['js']  = 'application/javascript' unless m_types.has_key?('js')
  m_types['svg'] = 'image/svg+xml'          unless m_types.has_key?('svg')

  server = WEBrick::HTTPServer.new(
    :Port          =>  options[:port],
    :BindAddress   =>  options[:allowall] ? '0.0.0.0' : '127.0.0.1',
    :MimeTypes     =>  m_types,
    :DocumentRoot  =>  options[:serverroot]
  )

  server.mount(
    INFO_PATH,
    Info,
    options[:usagemessage]
  )

  server.mount(
    UPLOAD_PATH,
    FileUpload,
    options[:networkdelay],
    options[:savedirectory]
  )

  ['TERM', 'INT'].each { |signal| trap(signal){ server.shutdown } }

  server.start
end