Class: Opal::CliRunners::Server
- Inherits:
-
Object
- Object
- Opal::CliRunners::Server
- Defined in:
- opal/lib/opal/cli_runners/server.rb
Defined Under Namespace
Classes: App
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#static_folder ⇒ Object
readonly
Returns the value of attribute static_folder.
Class Method Summary collapse
Instance Method Summary collapse
- #build_app(source) ⇒ Object
- #exit_status ⇒ Object
-
#initialize(data) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
Constructor Details
#initialize(data) ⇒ Server
Returns a new instance of Server.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'opal/lib/opal/cli_runners/server.rb', line 12 def initialize(data) = data[:options] || {} builder = data[:builder] @code = builder.to_s + "\n" + builder.source_map.to_data_uri_comment @argv = data[:argv] || [] @output = data[:output] || $stdout @port = .fetch(:port, ENV['OPAL_CLI_RUNNERS_SERVER_PORT'] || 3000).to_i @static_folder = [:static_folder] || ENV['OPAL_CLI_RUNNERS_SERVER_STATIC_FOLDER'] @static_folder = @static_folder == true ? 'public' : @static_folder @static_folder = File.(@static_folder) if @static_folder end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
28 29 30 |
# File 'opal/lib/opal/cli_runners/server.rb', line 28 def argv @argv end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
28 29 30 |
# File 'opal/lib/opal/cli_runners/server.rb', line 28 def code @code end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
28 29 30 |
# File 'opal/lib/opal/cli_runners/server.rb', line 28 def output @output end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
28 29 30 |
# File 'opal/lib/opal/cli_runners/server.rb', line 28 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
28 29 30 |
# File 'opal/lib/opal/cli_runners/server.rb', line 28 def server @server end |
#static_folder ⇒ Object (readonly)
Returns the value of attribute static_folder.
28 29 30 |
# File 'opal/lib/opal/cli_runners/server.rb', line 28 def static_folder @static_folder end |
Class Method Details
.call(data) ⇒ Object
6 7 8 9 10 |
# File 'opal/lib/opal/cli_runners/server.rb', line 6 def self.call(data) runner = new(data) runner.run runner.exit_status end |
Instance Method Details
#build_app(source) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'opal/lib/opal/cli_runners/server.rb', line 52 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_status ⇒ Object
48 49 50 |
# File 'opal/lib/opal/cli_runners/server.rb', line 48 def exit_status nil end |
#run ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'opal/lib/opal/cli_runners/server.rb', line 30 def run unless argv.empty? raise ArgumentError, 'Program arguments are not supported on the Server runner' end require 'rack' require 'logger' app = build_app(code) @server = Rack::Server.start( app: app, Port: port, AccessLog: [], Logger: Logger.new(output), ) end |