Class: Teabag::Server

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

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



8
9
10
11
# File 'lib/teabag/server.rb', line 8

def initialize
  @port = find_available_port
  Thin::Logging.silent = true if defined?(Thin)
end

Instance Method Details

#portObject



42
43
44
# File 'lib/teabag/server.rb', line 42

def port
  @port
end

#responsive?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/teabag/server.rb', line 30

def responsive?
  return false if @thread && @thread.join(0)
  TCPSocket.new("127.0.0.1", port).close
  return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  return false
end

#startObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/teabag/server.rb', line 13

def start
  STDOUT.print "Starting the Teabag server...\n" unless Teabag.configuration.suppress_log
  @thread = Thread.new do
    server = Rack::Server.new(rack_options)
    server.start
  end
  wait_until_started
rescue => e
  raise "Cannot start server: #{e.message}"
end

#urlObject



38
39
40
# File 'lib/teabag/server.rb', line 38

def url
  "http://127.0.0.1:#{port}"
end

#wait_until_startedObject



24
25
26
27
28
# File 'lib/teabag/server.rb', line 24

def wait_until_started
  Timeout.timeout(Teabag.configuration.server_timeout) { @thread.join(0.1) until responsive? }
rescue Timeout::Error
  raise "Server failed to start. You may need to increase the timeout configuration."
end