Class: Sbmt::Pact::Provider::PactBrokerProxyRunner

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

Constant Summary collapse

FILTER_TYPE_NONE =
nil
FILTER_TYPE_GRPC =
:grpc
FILTER_TYPE_ASYNC =
:async
FILTER_TYPE_SYNC =
:sync
FILTER_TYPE_HTTP =
:http

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PactBrokerProxyRunner.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sbmt/pact/provider/pact_broker_proxy_runner.rb', line 17

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

  @thread = nil
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#proxy_urlObject



29
30
31
# File 'lib/sbmt/pact/provider/pact_broker_proxy_runner.rb', line 29

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

#runObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/sbmt/pact/provider/pact_broker_proxy_runner.rb', line 58

def run
  start

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

#startObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sbmt/pact/provider/pact_broker_proxy_runner.rb', line 33

def start
  raise "server already running, stop server before starting new one" if @thread

  @server = WEBrick::HTTPServer.new({BindAddress: @host, Port: @port}, WEBrick::Config::HTTP)
  @server.mount("/", Rack::Handler::WEBrick, PactBrokerProxy.new(
    nil,
    backend: @pact_broker_host, streaming: false, filter_type: @filter_type,
    username: @pact_broker_user, password: @pact_broker_password, logger: @logger
  ))

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

#stopObject



49
50
51
52
53
54
55
56
# File 'lib/sbmt/pact/provider/pact_broker_proxy_runner.rb', line 49

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

  @server&.shutdown
  @thread&.join

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