Sha256: 8b28ba4098361ef4619499f9f0bb1a803552f151ebd0790d9620ece7cd68edb9

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

require 'rack/handler/webrick'

module GeneValidatorApp
  # Simple wrapper around WEBrick and Rack::Handler::WEBrick to host
  # GeneValidatorApp 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
      [: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

5 entries across 5 versions & 1 rubygems

Version Path
genevalidatorapp-1.5.4 lib/genevalidatorapp/server.rb
genevalidatorapp-1.5.3 lib/genevalidatorapp/server.rb
genevalidatorapp-1.5.2 lib/genevalidatorapp/server.rb
genevalidatorapp-1.5.1 lib/genevalidatorapp/server.rb
genevalidatorapp-1.5.0 lib/genevalidatorapp/server.rb