Class: Pact::Server
- Inherits:
-
Object
- Object
- Pact::Server
- Defined in:
- lib/pact/consumer/server.rb
Defined Under Namespace
Classes: Middleware
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
Instance Method Summary collapse
- #boot ⇒ Object
- #error ⇒ Object
- #get_identity ⇒ Object
-
#initialize(app, host, port, options = {}) ⇒ Server
constructor
A new instance of Server.
- #reset_error! ⇒ Object
- #responsive? ⇒ Boolean
- #run_default_server(app, port) ⇒ Object
- #ssl_opts ⇒ Object
- #webrick_opts ⇒ Object
Constructor Details
#initialize(app, host, port, options = {}) ⇒ Server
Returns a new instance of Server.
39 40 41 42 43 44 45 46 |
# File 'lib/pact/consumer/server.rb', line 39 def initialize(app, host, port, = {}) @app = app @middleware = Middleware.new(@app) @server_thread = nil @host = host @port = port @options = end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
37 38 39 |
# File 'lib/pact/consumer/server.rb', line 37 def app @app end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
37 38 39 |
# File 'lib/pact/consumer/server.rb', line 37 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
37 38 39 |
# File 'lib/pact/consumer/server.rb', line 37 def @options end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
37 38 39 |
# File 'lib/pact/consumer/server.rb', line 37 def port @port end |
Class Method Details
.ports ⇒ Object
32 33 34 |
# File 'lib/pact/consumer/server.rb', line 32 def ports @ports ||= {} end |
Instance Method Details
#boot ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/pact/consumer/server.rb', line 99 def boot unless responsive? @server_thread = Thread.new { run_default_server(@middleware, @port) } Timeout.timeout(60) { @server_thread.join(0.1) until responsive? } end rescue Timeout::Error raise "Rack application timed out during boot" else Pact::Server.ports[@app.object_id] = @port self end |
#error ⇒ Object
52 53 54 |
# File 'lib/pact/consumer/server.rb', line 52 def error @middleware.error end |
#get_identity ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/pact/consumer/server.rb', line 75 def get_identity return false unless @port http = Net::HTTP.new host, @port if [:ssl] http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.get('/__identify__') end |
#reset_error! ⇒ Object
48 49 50 |
# File 'lib/pact/consumer/server.rb', line 48 def reset_error! @middleware.error = nil end |
#responsive? ⇒ Boolean
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pact/consumer/server.rb', line 56 def responsive? return false if @server_thread && @server_thread.join(0) res = get_identity if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection) return res.body == @app.object_id.to_s end rescue SystemCallError return false rescue EOFError return false end |
#run_default_server(app, port) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/pact/consumer/server.rb', line 68 def run_default_server(app, port) require 'rack/handler/webrick' Rack::Handler::WEBrick.run(app, **webrick_opts) do |server| @port = server[:Port] end end |
#ssl_opts ⇒ Object
95 96 97 |
# File 'lib/pact/consumer/server.rb', line 95 def ssl_opts { SSLEnable: true, SSLCertName: [ ["CN", host] ] } end |
#webrick_opts ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/pact/consumer/server.rb', line 85 def webrick_opts opts = { Host: host.nil? ? 'localhost' : host, Port: port.nil? ? 0 : port, AccessLog: [], Logger: WEBrick::Log::new(nil, 0) } opts.merge!({ :SSLCertificate => OpenSSL::X509::Certificate.new(File.open([:sslcert]).read) }) if [:sslcert] opts.merge!({ :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.open([:sslkey]).read) }) if [:sslkey] opts.merge!(ssl_opts) if [:ssl] opts end |