Class: Opal::CliRunners::Server

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

Defined Under Namespace

Classes: App

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Server

Returns a new instance of Server



6
7
8
9
10
11
12
13
14
# File 'opal/lib/opal/cli_runners/server.rb', line 6

def initialize(options)
  @output = options.fetch(:output, $stdout)

  @port = options.fetch(:port, ENV['OPAL_CLI_RUNNERS_SERVER_PORT'] || 3000).to_i

  @static_folder = options[:static_folder] || ENV['OPAL_CLI_RUNNERS_SERVER_STATIC_FOLDER']
  @static_folder = @static_folder == true ? 'public' : @static_folder
  @static_folder = File.expand_path(@static_folder) if @static_folder
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output



15
16
17
# File 'opal/lib/opal/cli_runners/server.rb', line 15

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port



15
16
17
# File 'opal/lib/opal/cli_runners/server.rb', line 15

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server



15
16
17
# File 'opal/lib/opal/cli_runners/server.rb', line 15

def server
  @server
end

#static_folderObject (readonly)

Returns the value of attribute static_folder



15
16
17
# File 'opal/lib/opal/cli_runners/server.rb', line 15

def static_folder
  @static_folder
end

Instance Method Details

#build_app(source) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'opal/lib/opal/cli_runners/server.rb', line 40

def build_app(source)
  app = App.new(source)

  if static_folder
    not_found = [404, {}, []]
    app = Rack::Cascade.new(
      [
        Rack::Static.new(->(_) { not_found }, urls: [''], root: static_folder),
        app
      ],
    )
  end

  app
end

#exit_statusObject



36
37
38
# File 'opal/lib/opal/cli_runners/server.rb', line 36

def exit_status
  nil
end

#run(source, argv) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'opal/lib/opal/cli_runners/server.rb', line 17

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'

  app = build_app(source)

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