Sha256: 3cb0cafda884cb84a2a3830ab0daee274fee748d2d4b64c4b358f97f6a84b9c8

Contents?: true

Size: 1.81 KB

Versions: 17

Compression:

Stored size: 1.81 KB

Contents

require 'pact/mock_service/control_server/app'

module Pact
  module MockService
    module ControlServer
      class Run

        def self.call options
          new(options).call
        end

        def initialize options
          @options = options
        end

        def call
          trap(:INT) { shutdown }
          trap(:TERM) { shutdown }

          # https://github.com/rack/rack/blob/ae78184e5c1fcf4ac133171ed4b47b0548cd9b44/lib/rack/handler/webrick.rb#L32
          # Rack adapter for webrick uses class variable for the server which contains the port,
          # so if we use it more than once in the same process, we lose the reference to the first
          # server, and can't shut it down. So, keep a manual reference to the Webrick server, and
          # shut it down directly rather than use Rack::Handler::WEBrick.shutdown
          # Ruby!
          Rack::Handler::WEBrick.run(control_server, webbrick_opts) do | server |
            @webrick_server = server
          end
        end

        private

        attr_reader :options

        def control_server
          @control_server ||= Pact::MockService::ControlServer::App.new control_server_options
        end

        def shutdown
          unless @shutting_down
            @shutting_down = true
            begin
              @control_server.shutdown
            ensure
              @webrick_server.shutdown
            end
          end
        end

        def control_server_options
          {
            log_dir: options[:log_dir] || "log",
            pact_dir: options[:pact_dir] || ".",
            cors_enabled: options[:cors] || false,
            ssl: options[:ssl]
          }
        end

        def webbrick_opts
          {
            :Port => options[:port],
            :AccessLog => []
          }
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pact-mock_service-0.8.1 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.8.0 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.7.2 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.7.1 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.7.0 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.6.0 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.5.5 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.5.4 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.5.3 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.5.2 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.5.1 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.5.0 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.4.2 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.4.1 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.4.0 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.3.0 lib/pact/mock_service/control_server/run.rb
pact-mock_service-0.2.4 lib/pact/mock_service/control_server/run.rb