class Opal::CliRunners::Server

Attributes

argv[R]
builder[R]
output[R]
port[R]
server[R]
static_folder[R]

Public Class Methods

call(data) click to toggle source
# File lib/opal/cli_runners/server.rb, line 8
def self.call(data)
  runner = new(data)
  runner.run
  runner.exit_status
end
new(data) click to toggle source
# 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

Public Instance Methods

build_app(builder) click to toggle source
# 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_status() click to toggle source
# File lib/opal/cli_runners/server.rb, line 49
def exit_status
  nil
end
run() click to toggle source
# 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