Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow padrino start to take handler specific options #1452

Merged
merged 2 commits into from Oct 16, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 additions & 1 deletion padrino-core/lib/padrino-core/cli/base.rb
Expand Up @@ -18,11 +18,19 @@ class Base < Thor
method_option :daemonize, :type => :boolean, :aliases => "-d", :desc => "Run daemonized in the background."
method_option :pid, :type => :string, :aliases => "-i", :desc => "File to store pid."
method_option :debug, :type => :boolean, :desc => "Set debugging flags."
method_option :options, :type => :array, :aliases => "-O", :desc => "--options NAME=VALUE NAME2=VALUE2'. pass VALUE to the server as option NAME. If no VALUE, sets it to true. Run '#{$0} --server_options"
method_option :server_options, :type => :boolean, :desc => "Tells the current server handler's options that can be used with --options"

def start
prepare :start
require File.expand_path("../adapter", __FILE__)
require File.expand_path('config/boot.rb')
Padrino::Cli::Adapter.start(options)

if options[:server_options]
puts server_options(options)
else
Padrino::Cli::Adapter.start(options)
end
end

desc "stop", "Stops the Padrino application (alternatively use 'st')."
Expand Down Expand Up @@ -124,6 +132,29 @@ def prepare(task)
end
end

# https://github.com/rack/rack/blob/master/lib/rack/server.rb\#L100
def server_options(options)
begin
info = []
server = Rack::Handler.get(options[:server]) || Rack::Handler.default(options)
if server && server.respond_to?(:valid_options)
info << ""
info << "Server-specific options for #{server.name}:"

has_options = false
server.valid_options.each do |name, description|
next if name.to_s.match(/^(Host|Port)[^a-zA-Z]/) # ignore handler's host and port options, we do our own.
info << " -O %-21s %s" % [name, description]
has_options = true
end
return "" if !has_options
end
info.join("\n")
rescue NameError
return "Warning: Could not find handler specified (#{options[:server] || 'default'}) to determine handler-specific options"
end
end

protected

def self.banner(task=nil, *args)
Expand Down
1 change: 1 addition & 0 deletions padrino-core/lib/padrino-core/server.rb
Expand Up @@ -31,6 +31,7 @@ def self.start(app, opts={})
FileUtils.mkdir_p(File.dirname(options[:pid]))
end
options[:server] = detect_rack_handler if options[:server].blank?
options.merge!(Hash[*options.delete(:options).map {|opt| opt.split('=',2)}.flatten].symbolize_keys!) if options[:options].is_a?(Array)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might I suggest breaking this out into a few lines? Looks very intimidating at first glance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense... they only question is how :) Maybe?:

if options[:options].is_a?(Array)
  server_options = Hash[*options.delete(:options).map {|opt| opt.split('=',2)}.flatten].symbolize_keys!
  options.merge!(server_options)
end

or

if options[:options].is_a?(Array)
  parsed_server_options = options.delete(:options).map { |opt| opt.split('=', 2) }.flatten
  server_options = Hash[*parsed_options].symbolize_keys!
  options.merge!(server_options)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if options[:options].is_a?(Array)
  parsed_server_options = options.delete(:options).map { |opt| opt.split('=', 2) }.flatten
  server_options = Hash[*parsed_options].symbolize_keys!
  options.merge!(server_options)
end

👍
More than acceptable

new(options, app).start
end

Expand Down