Class: Padrino::Server
- 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
-
#app ⇒ Object
(also: #wrapped_app)
readonly
The application the server will run.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.detect_address(options) ⇒ Object
Detects Host and Port for Rack server.
-
.detect_rack_handler ⇒ Object
Detects the supported handler to use.
-
.parse_server_options(options) ⇒ Object
Parses an array of server options.
-
.prepare_pid(pid) ⇒ Object
Prepares a directory for pid file.
-
.start(app, options = {}) ⇒ Object
Starts the application on the available server with specified options.
Instance Method Summary collapse
-
#initialize(options, app) ⇒ Server
constructor
A new instance of Server.
-
#start ⇒ Object
Starts the application on the available server with specified options.
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(, app) @options, @app = , app end |
Instance Attribute Details
#app ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
67 68 69 |
# File 'padrino-core/lib/padrino-core/server.rb', line 67 def @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() address = DEFAULT_ADDRESS.merge(.select { |key| %i[Host Port].include?(key) }) address[:Host] = [:host] if [:host] address[:Port] = [:port] if [:port] address end |
.detect_rack_handler ⇒ Object
Detects the supported handler to use.
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.() = Array().flat_map { |option| option.split('=', 2) } Utils.symbolize_keys(Hash[*]) 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.(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, = {}) = Utils.symbolize_keys(.to_hash) .update((.delete(:options))) .update(detect_address()) [:pid] = prepare_pid([:pid]) if [:daemonize] [:server] ||= detect_rack_handler # disable Webrick AccessLog [:AccessLog] = [] new(, app).start end |
Instance Method Details
#start ⇒ Object
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://#{[:Host]}:#{[: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 [:daemonize] end |