Sha256: c9ccec5a4fed93972d115b9d5adbf59c7c0b24f8aad452a63bbd2c02477333a7

Contents?: true

Size: 1.44 KB

Versions: 15

Compression:

Stored size: 1.44 KB

Contents

require 'rack/handler/webrick'

module SequenceServer
  # Simple wrapper around WEBrick and Rack::Handler::WEBrick to host
  # SequenceServer standalone.
  class Server
    class << self
      def run(*args)
        new(*args).start
      end
    end

    def initialize(app)
      @app = app
    end

    attr_reader :app

    # Start server. Raises Errno::EADDRINUSE if port is in use by another
    # process. Raises Errno::EACCES if binding to the port requires root
    # privilege.
    def start
      setup_signal_handlers
      @server = WEBrick::HTTPServer.new(options)
      @server.mount '/', Rack::Handler::WEBrick, app
      @server.start
    end

    # Stop server.
    def stop
      @server.shutdown
    end

    # Options Hash passed to WEBrick::HTTPServer.
    # rubocop:disable Metrics/AbcSize
    def options
      @options ||= {
        BindAddress:      app.config[:host],
        Port:             app.config[:port],
        StartCallback:    proc { app.on_start },
        StopCallback:     proc { app.on_stop  },
        OutputBufferSize: 5,
        AccessLog:        [[logdev, WEBrick::AccessLog::COMMON_LOG_FORMAT]],
        Logger:           WEBrick::Log.new(logdev)
      }
    end
    # rubocop:enable Metrics/AbcSize

    private

    def setup_signal_handlers
      %i[INT TERM].each do |sig|
        trap sig do
          stop
        end
      end
    end

    def logdev
      @logdev ||= app.verbose? ? STDERR : '/dev/null'
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sequenceserver-2.2.0 lib/sequenceserver/server.rb
sequenceserver-2.1.0 lib/sequenceserver/server.rb
sequenceserver-2.0.0 lib/sequenceserver/server.rb
sequenceserver-2.0.0.rc8 lib/sequenceserver/server.rb
sequenceserver-2.0.0.rc7 lib/sequenceserver/server.rb
sequenceserver-2.0.0.rc6 lib/sequenceserver/server.rb
sequenceserver-2.0.0.rc5 lib/sequenceserver/server.rb
sequenceserver-2.0.0.rc4 lib/sequenceserver/server.rb
sequenceserver-2.0.0.rc3 lib/sequenceserver/server.rb
sequenceserver-2.0.0.rc2 lib/sequenceserver/server.rb
sequenceserver-2.0.0.rc1 lib/sequenceserver/server.rb
sequenceserver-2.0.0.beta4 lib/sequenceserver/server.rb
sequenceserver-2.0.0.beta3 lib/sequenceserver/server.rb
sequenceserver-2.0.0.beta1 lib/sequenceserver/server.rb
sequenceserver-1.1.0.beta12 lib/sequenceserver/server.rb