Class: Opal::CliRunners::Server

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

Defined Under Namespace

Classes: App

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Server

Returns a new instance of Server.



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

def initialize(data)
  options = data[:options] || {}
  @builder = data[:builder]

  @argv = data[:argv] || []

  @output = data[: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

#argvObject (readonly)

Returns the value of attribute argv.



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

def argv
  @argv
end

#builderObject (readonly)

Returns the value of attribute builder.



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

def builder
  @builder
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#static_folderObject (readonly)

Returns the value of attribute static_folder.



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

def static_folder
  @static_folder
end

Class Method Details

.call(data) ⇒ Object



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

def self.call(data)
  runner = new(data)
  runner.run
  runner.exit_status
end

Instance Method Details

#build_app(builder) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/opal/cli_runners/server.rb', line 53

def build_app(builder)
  app = App.new(builder: builder, main: 'cli-runner')

  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



49
50
51
# File 'lib/opal/cli_runners/server.rb', line 49

def exit_status
  nil
end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/opal/cli_runners/server.rb', line 31

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

  require 'rack'
  require 'logger'

  app = build_app(builder)

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