Sha256: 79af13cc71cbfa45f1c598385a20ec314992f52a905cb2f05e7cf8f53b3d02c9

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 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
      %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

7 entries across 7 versions & 1 rubygems

Version Path
genevalidatorapp-2.1.8 lib/genevalidatorapp/server.rb
genevalidatorapp-2.1.7 lib/genevalidatorapp/server.rb
genevalidatorapp-2.1.6 lib/genevalidatorapp/server.rb
genevalidatorapp-2.1.5 lib/genevalidatorapp/server.rb
genevalidatorapp-2.1.4 lib/genevalidatorapp/server.rb
genevalidatorapp-2.1.3 lib/genevalidatorapp/server.rb
genevalidatorapp-2.1.2 lib/genevalidatorapp/server.rb