Class: RealWeb::Server
- Inherits:
-
Object
- Object
- RealWeb::Server
- Defined in:
- lib/realweb/server.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_PORT_RANGE =
8000..10000
- DEFAULT_HOST =
'127.0.0.1'
- DEFAULT_LOGGER =
Logger.new(StringIO.new)
- DEFAULT_TIMEOUT =
seconds
2
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Class Method Summary collapse
-
.server_ready?(server) ⇒ Boolean
return true if available, false if still waiting.
- .with_rackup(*args) ⇒ Object
Instance Method Summary collapse
- #base_uri ⇒ Object
-
#initialize(config_ru, options = {}) {|_self| ... } ⇒ Server
constructor
Create a RealWeb::Server object, either a RealWeb::ForkingServer or a RealWeb::ThreadServer.
- #port ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(config_ru, options = {}) {|_self| ... } ⇒ Server
Create a RealWeb::Server object, either a RealWeb::ForkingServer or a RealWeb::ThreadServer.
:port_range - Range specifying acceptable tcp ports to boot on. :logger - An instance of Logger. :host - Alternative host. Default is 127.0.0.1. :verbose - Print server logs and errors to stdout/err :timeout - Timout in seconds to wait for the server to boot. :ready - A Proc that returns true when the server is ready, or false
if it is not ready yet. This proc will be called every 100ms with the
server as an argument. If blank, an open-uri based check will be used.
:pre_spawn_callback - a lambda to be called with the server object before
spawning the server.
Any remaining options will be passed to Rack::Server on boot.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/realweb/server.rb', line 50 def initialize(config_ru, = {}) @config_ru = config_ru @running = false @port_range = .delete(:port_range) || DEFAULT_PORT_RANGE @logger = .delete(:logger) || DEFAULT_LOGGER @host = .delete(:host) || DEFAULT_HOST @verbose = .delete(:verbose) || false @timeout = .delete(:timeout) || DEFAULT_TIMEOUT @ready = .delete(:ready) || self.class.method(:server_ready?) @pre_spawn_callback = .delete(:pre_spawn_callback) @rack_options = yield self if block_given? end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/realweb/server.rb', line 10 def host @host end |
Class Method Details
.server_ready?(server) ⇒ Boolean
return true if available, false if still waiting
18 19 20 21 22 23 24 25 |
# File 'lib/realweb/server.rb', line 18 def self.server_ready?(server) open(server.base_uri) true rescue OpenURI::HTTPError true rescue Errno::ECONNREFUSED => e false end |
.with_rackup(*args) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/realweb/server.rb', line 27 def self.with_rackup(*args) new(*args) do |server| server.start yield server if block_given? server.stop end end |
Instance Method Details
#base_uri ⇒ Object
87 88 89 |
# File 'lib/realweb/server.rb', line 87 def base_uri URI.parse("http://#{host}:#{port}/") end |
#port ⇒ Object
67 68 69 |
# File 'lib/realweb/server.rb', line 67 def port @port ||= find_port end |
#running? ⇒ Boolean
71 72 73 |
# File 'lib/realweb/server.rb', line 71 def running? @running end |
#start ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/realweb/server.rb', line 75 def start return if running? run_pre_spawn spawn_server wait_for_server @running = true end |
#stop ⇒ Object
83 84 85 |
# File 'lib/realweb/server.rb', line 83 def stop @running = false end |