Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #201 from nolman/repl2

adding a console to goliath
  • Loading branch information...
commit dfa85595130ab8b4f9745203658f2e646a593da2 2 parents 9040568 + b140e52
@dj2 dj2 authored
View
20 lib/goliath/console.rb
@@ -0,0 +1,20 @@
+module Goliath
+ # The console execution class for Goliath. This will load a REPL inside of a
+ # running reactor with the associated Goliath config loaded.
+ #
+ # @private
+ class Console
+ # Starts the reactor and the REPL.
+ #
+ # @return [Nil]
+ def self.run!(server)
+ require 'irb'
+ EM.synchrony do
+ server.load_config
+ Object.send(:define_method, :goliath_server) { server }
+ IRB.start
+ EM.stop
+ end
+ end
+ end
+end
View
28 lib/goliath/runner.rb
@@ -1,5 +1,6 @@
require 'goliath/goliath'
require 'goliath/server'
+require 'goliath/console'
require 'optparse'
require 'log4r'
@@ -167,6 +168,7 @@ def options_parser
opts.separator ""
opts.separator "Common options:"
+ opts.on('-C', '--console', 'Start a console') { @options[:console] = true }
opts.on('-v', '--verbose', "Enable verbose logging (default: #{@options[:verbose]})") { |v| @options[:verbose] = v }
opts.on('-h', '--help', 'Display help message') { show_options(opts) }
end
@@ -185,6 +187,11 @@ def load_plugins(plugins)
#
# @return [Nil]
def run
+ if options[:console]
+ Goliath::Console.run!(setup_server)
+ return
+ end
+
unless Goliath.env?(:test)
$LOADED_FEATURES.unshift(File.basename($0))
Dir.chdir(File.expand_path(File.dirname($0)))
@@ -241,6 +248,20 @@ def setup_logger
log
end
+ # Sets up the Goliath server
+ #
+ # @param log [Logger] The logger to configure the server to log to
+ # @return [Server] an instance of a Goliath server
+ def setup_server(log = setup_logger)
+ server = Goliath::Server.new(@address, @port)
+ server.logger = log
+ server.app = @app
+ server.api = @api
+ server.plugins = @plugins || []
+ server.options = @server_options
+ server
+ end
+
# Setup file logging
#
# @param log [Logger] The logger to add file logging too
@@ -271,12 +292,7 @@ def run_server
log.info("Starting server on #{@address}:#{@port} in #{Goliath.env} mode. Watch out for stones.")
- server = Goliath::Server.new(@address, @port)
- server.logger = log
- server.app = @app
- server.api = @api
- server.plugins = @plugins || []
- server.options = @server_options
+ server = setup_server(log)
server.start
end
View
18 spec/unit/console_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+require 'goliath/runner'
+require 'irb'
+
+describe Goliath::Console do
+ before(:each) do
+ @server = Goliath::Server.new
+ end
+
+ describe 'run!' do
+ it "starts a irb session" do
+ Object.should_receive(:send).with(:define_method, :goliath_server)
+ IRB.should_receive(:start)
+ @server.should_receive(:load_config)
+ Goliath::Console.run!(@server)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.