Module: OddJob

Defined in:
lib/oddjob.rb,
lib/oddjob/version.rb

Defined Under Namespace

Modules: HtmlRender Classes: FileUpload, Info

Constant Summary collapse

UPLOAD_PATH =
'/oj_upload'
INFO_PATH =
'/oj_info'
DEFAULT_PORT =
4400
VERSION =
'1.8.0'

Class Method Summary collapse

Class Method Details

.server(opts) ⇒ Object

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).



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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