Sha256: bc383ff2d49f0bef67d73e6b9df936b61c3d0a7e1768f53fbd33a6f6fe20f96e

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require 'reel'

module Rack
  module Handler
    class Reel
      attr_reader :options

      # Don't mess with Rack::File
      File = ::File

      DEFAULT_OPTIONS = {
        :host    => "0.0.0.0",
        :port    => 3000,
        :quiet   => false,
        :workers => 10,
        :rackup  => "config.ru"
      }

      def self.run(app, options = {})

        handler = Reel.new(options)

        ::Reel::Logger.info "A Reel good HTTP server!"
        ::Reel::Logger.info "Listening on #{handler[:host]}:#{handler[:port]}"

        handler.start
      end

      def initialize(opts = {})
        opts  = normalize_options(opts)

        @options = DEFAULT_OPTIONS.merge(opts)

        if @options[:environment]
          ENV['RACK_ENV'] = @options[:environment].to_s
        end
      end

      def start
        Celluloid::Actor[:reel_rack_pool] = ::Reel::RackWorker.pool(size: options[:workers], args: [self])

        ::Reel::Server.supervise_as(:reel_server, options[:host], options[:port]) do |connection|
          Celluloid::Actor[:reel_rack_pool].handle(connection.detach)
        end

        sleep
      end

      def [](option)
        @options[option]
      end

      def rack_app
        return @options[:app] if @options[:app]

        path = @options[:rackup]

        unless File.exists?(path)
          raise "Missing rackup file '#{path}'"
        end

        @options[:app], options = Rack::Builder.parse_file path
        @options.merge! options

        unless @options[:quiet]
          @options[:app] = Rack::CommonLogger.new(@options[:app], STDOUT)
        end

        @options[:app]
      end

      private

      # Transform the options that rails s reel passes
      def normalize_options(options)
        options.inject({}) { |h, (k,v)|  h[k.downcase] = v ; h }
        options[:rackup] = options[:config] if options[:config]
        options
      end
    end

    register :reel, Reel
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reel-0.2.0 lib/rack/handler/reel.rb
reel-0.2.0.pre lib/rack/handler/reel.rb