Class: Opal::CliRunners::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Server

Returns a new instance of Server



6
7
8
9
# File 'opal/lib/opal/cli_runners/server.rb', line 6

def initialize(options)
  @output = options.fetch(:output, $stdout)
  @port = options.fetch(:port, 3000).to_int
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output



10
11
12
# File 'opal/lib/opal/cli_runners/server.rb', line 10

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port



10
11
12
# File 'opal/lib/opal/cli_runners/server.rb', line 10

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server



10
11
12
# File 'opal/lib/opal/cli_runners/server.rb', line 10

def server
  @server
end

Instance Method Details

#app(source) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'opal/lib/opal/cli_runners/server.rb', line 33

def app(source)
  lambda do |env|
    case env['PATH_INFO']
    when '/'
      body = <<-HTML
      <!doctype html>
      <html>
        <head>
          <meta charset="utf-8"/>
          <script src="/cli_runner.js"></script>
        </head>
      </html>
      HTML
      [200, {}, [body]]
    when '/cli_runner.js'
      [200, {'Content-Type' => 'text/javascript'}, [source]]
    else
      [404, {}, [body]]
    end
  end
end

#exit_statusObject



29
30
31
# File 'opal/lib/opal/cli_runners/server.rb', line 29

def exit_status
  nil
end

#run(source, argv) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'opal/lib/opal/cli_runners/server.rb', line 12

def run(source, argv)
  unless argv.empty?
    raise ArgumentError, 'Program arguments are not supported on the Server runner'
  end

  require 'rack'
  require 'webrick'
  require 'logger'

  @server = Rack::Server.start(
    :app       => app(source),
    :Port      => port,
    :AccessLog => [],
    :Logger    => Logger.new(output)
  )
end