Class: Padrino::Server

Inherits:
Rackup::Server
  • Object
show all
Defined in:
padrino-core/lib/padrino-core/server.rb

Overview

This module builds a Padrino server to run the project based on available handlers.

Constant Summary collapse

DEFAULT_ADDRESS =
{ :Host => '127.0.0.1', :Port => 3000 }
Handlers =

Server Handlers

[:thin, :puma, :'spider-gazelle', :mongrel, :trinidad, :webrick]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, app) ⇒ Server

Returns a new instance of Server.



51
52
53
# File 'padrino-core/lib/padrino-core/server.rb', line 51

def initialize(options, app)
  @options, @app = options, app
end

Instance Attribute Details

#appObject (readonly) Also known as: wrapped_app

The application the server will run.



67
68
69
# File 'padrino-core/lib/padrino-core/server.rb', line 67

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



70
71
72
# File 'padrino-core/lib/padrino-core/server.rb', line 70

def options
  @options
end

Class Method Details

.detect_address(options) ⇒ Object

Detects Host and Port for Rack server.



104
105
106
107
108
109
# File 'padrino-core/lib/padrino-core/server.rb', line 104

def self.detect_address(options)
  address = DEFAULT_ADDRESS.merge(options.select{ |key| [:Host, :Port].include?(key) })
  address[:Host] = options[:host] if options[:host]
  address[:Port] = options[:port] if options[:port]
  address
end

.detect_rack_handlerObject

Detects the supported handler to use.

Examples:

detect_rack_handler => <ThinHandler>


78
79
80
81
82
83
84
85
# File 'padrino-core/lib/padrino-core/server.rb', line 78

def self.detect_rack_handler
  Handlers.each do |handler|
    return handler if Rackup::Handler.get(handler.to_s.downcase)
  rescue LoadError, NameError
    # Ignored
  end
  fail "Server handler (#{Handlers.join(', ')}) not found."
end

.parse_server_options(options) ⇒ Object

Parses an array of server options.



97
98
99
100
# File 'padrino-core/lib/padrino-core/server.rb', line 97

def self.parse_server_options(options)
  parsed_server_options = Array(options).flat_map{ |option| option.split('=', 2) }
  Utils.symbolize_keys(Hash[*parsed_server_options])
end

.prepare_pid(pid) ⇒ Object

Prepares a directory for pid file.



89
90
91
92
93
# File 'padrino-core/lib/padrino-core/server.rb', line 89

def self.prepare_pid(pid)
  pid ||= 'tmp/pids/server.pid'
  FileUtils.mkdir_p(File.dirname(pid))
  File.expand_path(pid)
end

.start(app, options = {}) ⇒ Object

Starts the application on the available server with specified options.



40
41
42
43
44
45
46
47
48
49
# File 'padrino-core/lib/padrino-core/server.rb', line 40

def self.start(app, options={})
  options = Utils.symbolize_keys(options.to_hash)
  options.update(parse_server_options(options.delete(:options)))
  options.update(detect_address(options))
  options[:pid] = prepare_pid(options[:pid]) if options[:daemonize]
  options[:server] ||= detect_rack_handler
  # disable Webrick AccessLog
  options[:AccessLog] = []
  new(options, app).start
end

Instance Method Details

#startObject

Starts the application on the available server with specified options.



56
57
58
59
60
61
62
63
64
# File 'padrino-core/lib/padrino-core/server.rb', line 56

def start
  puts "=> Padrino/#{Padrino.version} has taken the stage #{Padrino.env} at http://#{options[:Host]}:#{options[:Port]}"
  [:INT, :TERM].each { |sig| trap(sig) { exit } }
  super do |server|
    server.threaded = true if server.respond_to?(:threaded=)
  end
ensure
  puts "<= Padrino leaves the gun, takes the cannoli" unless options[:daemonize]
end