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

%i[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.



48
49
50
# File 'padrino-core/lib/padrino-core/server.rb', line 48

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.



64
65
66
# File 'padrino-core/lib/padrino-core/server.rb', line 64

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.detect_address(options) ⇒ Object

Detects Host and Port for Rack server.



100
101
102
103
104
105
# File 'padrino-core/lib/padrino-core/server.rb', line 100

def self.detect_address(options)
  address = DEFAULT_ADDRESS.merge(options.select { |key| %i[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>


74
75
76
77
78
79
80
81
# File 'padrino-core/lib/padrino-core/server.rb', line 74

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

.parse_server_options(options) ⇒ Object

Parses an array of server options.



93
94
95
96
# File 'padrino-core/lib/padrino-core/server.rb', line 93

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.



85
86
87
88
89
# File 'padrino-core/lib/padrino-core/server.rb', line 85

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.



37
38
39
40
41
42
43
44
45
46
# File 'padrino-core/lib/padrino-core/server.rb', line 37

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.



53
54
55
56
57
58
59
60
61
# File 'padrino-core/lib/padrino-core/server.rb', line 53

def start
  puts "=> Padrino/#{Padrino.version} has taken the stage #{Padrino.env} at http://#{options[:Host]}:#{options[:Port]}"
  %i[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