Sha256: a45cfdbd5c777b666080db1a204fe0d191ba25afb6db5e58a18a81f665690776

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

require 'crystal/rack/support'
require 'crystal/rack/rack_app'

module Crystal
  class Adapter
    SERVERS = %w{mongrel thin webrick}
    
    class Callback
      def initialize app; end
      def self.call env; Crystal::Adapter.call env end
    end
    
    class << self
      
      def run
        app = build_app
        handler = detect_rack_handler
        handler_name = handler.name.gsub(/.*::/, '')
        puts "  Crystal #{Crystal::VERSION} started on #{Crystal.setting.port} in #{Crystal.setting.environment} mode" unless handler_name =~/cgi/i
        handler.run app, :Host => Crystal.setting.host, :Port => Crystal.setting.port do |server|
          [:INT, :TERM].each {|sig| trap(sig){quit!(server, handler_name)}}
        end
      rescue Errno::EADDRINUSE => e
        puts "  #{port} is taken!"
      end
      
      def quit!(server, handler_name)
        ## Use thins' hard #stop! if available, otherwise just #stop
        server.respond_to?(:stop!) ? server.stop! : server.stop
        puts "\n  Crystal stopped" unless handler_name =~/cgi/i        
      end
      
      protected        
        def build_app
          builder = Rack::Builder.new
          builder.use Rack::Session::Cookie if Crystal.setting.session?
          # builder.use Rack::CommonLogger
          builder.use Rack::MethodOverride          
          # builder.use ShowExceptions if Crystal.setting.show_exceptions?

          # TODO add middleware here
          
          builder.run Crystal::RackApp.new
          builder.to_app
        end
      
        def detect_rack_handler
          SERVERS.each do |server_name|
            begin
              return Rack::Handler.get(server_name.downcase)
            rescue LoadError
            rescue NameError
            end
          end
          fail "  Server handler (#{servers.join(',')}) not found."
        end
        
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
crystal-0.0.3 lib/crystal/rack/adapter.rb
crystal-0.0.2 lib/crystal/rack/adapter.rb