Sha256: 996e1d268237b06db64eae889bc89961992fbdc0f0589e2d15f350414ccd188e

Contents?: true

Size: 1.93 KB

Versions: 17

Compression:

Stored size: 1.93 KB

Contents

module Padrino

  ##
  # Run the Padrino apps as a self-hosted server using:
  # thin, mongrel, webrick in that order.
  # 
  # ==== Examples
  # 
  #   Padrino.run! # with these defaults => host: "localhost", port: "3000", adapter: the first found
  #   Padrino.run!("localhost", "4000", "mongrel") # use => host: "localhost", port: "3000", adapter: "mongrel"
  # 
  def self.run!(*args)
    Padrino.load! unless Padrino.loaded?
    Server.build(*args)
  end

  ##
  # This module build a Padrino server
  # 
  module Server

    class LoadError < RuntimeError; end

    Handlers = %w[thin mongrel webrick] unless const_defined?(:Handlers)

    private
      def self.build(host="localhost", port=3000, adapter=nil)
        handler_name = adapter ? adapter.to_s.capitalize : detect_handler.name.gsub(/.*::/, '')

        begin
          handler = Rack::Handler.get(handler_name.downcase)
        rescue
          raise LoadError, "#{handler_name} not supported yet, available adapters are: #{Handlers.inspect}"
          exit
        end

        handler.run Padrino.application, :Host => host, :Port => port do |server|
          trap(:INT) do
            # Use thins' hard #stop! if available, otherwise just #stop
            server.respond_to?(:stop!) ? server.stop! : server.stop
            puts "<= Padrino has ended his set (crowd applauds)"
          end
        end
      rescue RuntimeError => e
        if e.message =~ /no acceptor/
          puts "=> Someone is already performing on port #{port}!"
        else
          raise e
        end
      rescue Errno::EADDRINUSE
        puts "=> Someone is already performing on port #{port}!"
      end

      def self.detect_handler
        Handlers.each do |server_name|
          begin
            return Rack::Handler.get(server_name.downcase)
          rescue Exception
          end
        end
        raise LoadError, "Server handler (#{servers.join(',')}) not found."
      end
  end # Server
end # Padrino

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
padrino-core-0.9.6 lib/padrino-core/server.rb
padrino-core-0.9.5 lib/padrino-core/server.rb
padrino-core-0.9.4 lib/padrino-core/server.rb
padrino-core-0.9.3 lib/padrino-core/server.rb
padrino-core-0.9.2 lib/padrino-core/server.rb
padrino-core-0.9.1 lib/padrino-core/server.rb
padrino-core-0.9.0 lib/padrino-core/server.rb
padrino-core-0.8.5 lib/padrino-core/server.rb
padrino-core-0.8.4 lib/padrino-core/server.rb
padrino-core-0.8.3 lib/padrino-core/server.rb
padrino-core-0.8.2 lib/padrino-core/server.rb
padrino-core-0.8.1 lib/padrino-core/server.rb
padrino-core-0.8.0 lib/padrino-core/server.rb
padrino-core-0.7.9 lib/padrino-core/server.rb
padrino-core-0.7.8 lib/padrino-core/server.rb
padrino-core-0.7.7 lib/padrino-core/server.rb
padrino-core-0.7.6 lib/padrino-core/server.rb