Class: Pact::Provider::PactBrokerProxyRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/provider/pact_broker_proxy_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pact_broker_host:, port: 9002, host: "127.0.0.1", pact_broker_user: nil, pact_broker_password: nil, pact_broker_token: nil, logger: nil) ⇒ PactBrokerProxyRunner

Returns a new instance of PactBrokerProxyRunner.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pact/provider/pact_broker_proxy_runner.rb', line 11

def initialize(pact_broker_host:, port: 9002, host: "127.0.0.1", pact_broker_user: nil, pact_broker_password: nil, pact_broker_token: nil, logger: nil)
  @host = host
  @port = port
  @pact_broker_host = pact_broker_host
  @pact_broker_user = pact_broker_user
  @pact_broker_password = pact_broker_password
  @pact_broker_token = pact_broker_token
  @logger = logger || Logger.new($stdout)

  @thread = nil
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/pact/provider/pact_broker_proxy_runner.rb', line 8

def logger
  @logger
end

Instance Method Details

#proxy_urlObject



23
24
25
# File 'lib/pact/provider/pact_broker_proxy_runner.rb', line 23

def proxy_url
  "http://#{@host}:#{@port}"
end

#runObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/pact/provider/pact_broker_proxy_runner.rb', line 66

def run
  start

  yield
rescue => e
  logger.fatal("FATAL ERROR: #{e.message} #{e.backtrace.join("\n")}")
  raise
ensure
  stop
end

#startObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pact/provider/pact_broker_proxy_runner.rb', line 27

def start
  raise "server already running, stop server before starting new one" if @thread
    # Rack 2/3 compatibility
  begin
    require 'rack/handler/webrick'
    handler = ::Rack::Handler::WEBrick
  rescue LoadError
    require 'rackup/handler/webrick'
    handler = Class.new(Rackup::Handler::WEBrick)
  end
  @server = WEBrick::HTTPServer.new(
    { BindAddress: @host, Port: @port, Logger: @logger, AccessLog: [] },
    WEBrick::Config::HTTP
    )
  @server.mount("/", handler, PactBrokerProxy.new(
    nil,
    backend: @pact_broker_host,
    streaming: false,
    username: @pact_broker_user || nil,
    password: @pact_broker_password || nil,
    token: @pact_broker_token || nil,
    logger: @logger
  ))

  @thread = Thread.new do
    @logger.debug "starting pact broker proxy server"
    @server.start
  end
end

#stopObject



57
58
59
60
61
62
63
64
# File 'lib/pact/provider/pact_broker_proxy_runner.rb', line 57

def stop
  @logger.info("stopping pact broker proxy server")

  @server&.shutdown
  @thread&.join

  @logger.info("pact broker proxy server stopped")
end