1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Split out config into explicit user and file parts

This commit is contained in:
schneems 2017-03-03 13:11:59 -08:00
parent e80b0cfd7e
commit cda9317e71
8 changed files with 97 additions and 205 deletions

View file

@ -47,21 +47,21 @@ module Puma
@parser.parse! @argv
if file = @argv.shift
@conf.configure do |c|
c.rackup file
@conf.configure do |user_config, file_config|
file_config.rackup file
end
end
rescue UnsupportedOption
exit 1
end
@conf.configure do |c|
@conf.configure do |user_config, file_config|
if @stdout || @stderr
c.stdout_redirect @stdout, @stderr, @append
user_config.stdout_redirect @stdout, @stderr, @append
end
if @control_url
c.activate_control_app @control_url, @control_options
user_config.activate_control_app @control_url, @control_options
end
end
@ -87,14 +87,14 @@ module Puma
#
def setup_options
@conf = Configuration.new do |c|
@conf = Configuration.new do |user_config, file_config|
@parser = OptionParser.new do |o|
o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
c.bind arg
user_config.bind arg
end
o.on "-C", "--config PATH", "Load PATH as a config file" do |arg|
c.load arg
file_config.load arg
end
o.on "--control URL", "The bind url to use for the control server",
@ -112,21 +112,21 @@ module Puma
end
o.on "-d", "--daemon", "Daemonize the server into the background" do
c.daemonize
c.quiet
user_config.daemonize
user_config.quiet
end
o.on "--debug", "Log lowlevel debugging information" do
c.debug
user_config.debug
end
o.on "--dir DIR", "Change to DIR before starting" do |d|
c.directory d
user_config.directory d
end
o.on "-e", "--environment ENVIRONMENT",
"The environment to run the Rack app on (default development)" do |arg|
c.environment arg
user_config.environment arg
end
o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
@ -135,50 +135,50 @@ module Puma
o.on "-p", "--port PORT", "Define the TCP port to bind to",
"Use -b for more advanced options" do |arg|
c.bind "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
user_config.bind "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
end
o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
c.pidfile arg
user_config.pidfile arg
end
o.on "--preload", "Preload the app. Cluster mode only" do
c.preload_app!
user_config.preload_app!
end
o.on "--prune-bundler", "Prune out the bundler env if possible" do
c.prune_bundler
user_config.prune_bundler
end
o.on "-q", "--quiet", "Do not log requests internally (default true)" do
c.quiet
user_config.quiet
end
o.on "-v", "--log-requests", "Log requests as they occur" do
c.log_requests
user_config.log_requests
end
o.on "-R", "--restart-cmd CMD",
"The puma command to run during a hot restart",
"Default: inferred" do |cmd|
c.restart_command cmd
user_config.restart_command cmd
end
o.on "-S", "--state PATH", "Where to store the state details" do |arg|
c.state_path arg
user_config.state_path arg
end
o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg|
min, max = arg.split(":")
if max
c.threads min, max
user_config.threads min, max
else
c.threads min, min
user_config.threads min, min
end
end
o.on "--tcp-mode", "Run the app in raw TCP mode instead of HTTP mode" do
c.tcp_mode!
user_config.tcp_mode!
end
o.on "-V", "--version", "Print the version information" do
@ -188,11 +188,11 @@ module Puma
o.on "-w", "--workers COUNT",
"Activate cluster mode: How many worker processes to create" do |arg|
c.workers arg
user_config.workers arg
end
o.on "--tag NAME", "Additional text to display in process listing" do |arg|
c.tag arg
user_config.tag arg
end
o.on "--redirect-stdout FILE", "Redirect STDOUT to a specific file" do |arg|

View file

@ -13,121 +13,10 @@ module Puma
DefaultWorkerShutdownTimeout = 30
end
# class LeveledOptions
# def initialize(default_options, user_options)
# @cur = user_options
# @set = [@cur]
# @defaults = default_options.dup
# end
# def initialize_copy(other)
# @set = @set.map { |o| o.dup }
# @cur = @set.last
# end
# def shift
# @cur = {}
# @set << @cur
# end
# def reverse_shift
# @cur = {}
# @set.unshift(@cur)
# end
# def [](key)
# @set.reverse_each do |o|
# if o.key? key
# return o[key]
# end
# end
# v = @defaults[key]
# if v.respond_to? :call
# v.call
# else
# v
# end
# end
# def fetch(key, default=nil)
# val = self[key]
# return val if val
# default
# end
# attr_reader :cur
# def all_of(key)
# all = []
# @set.each do |o|
# if v = o[key]
# if v.kind_of? Array
# all += v
# else
# all << v
# end
# end
# end
# all
# end
# def []=(key, val)
# @cur[key] = val
# end
# def key?(key)
# @set.each do |o|
# if o.key? key
# return true
# end
# end
# @default.key? key
# end
# def merge!(o)
# o.each do |k,v|
# @cur[k]= v
# end
# end
# def flatten
# options = {}
# @set.each do |o|
# o.each do |k,v|
# options[k] ||= v
# end
# end
# options
# end
# def explain
# indent = ""
# @set.each do |o|
# o.keys.sort.each do |k|
# puts "#{indent}#{k}: #{o[k].inspect}"
# end
# indent = " #{indent}"
# end
# end
# def force_defaults
# @defaults.each do |k,v|
# if v.respond_to? :call
# @defaults[k] = v.call
# end
# end
# end
# end
# A class used for storing configuration options
# Options can be provided directly via the cli i.e. `puma -p 3001`
# or via a config file or multiple config files, or set as a default value
#
class UserFileDefaultOptions
def initialize(user_options, default_options)
@user_options = user_options
@ -147,6 +36,10 @@ module Puma
user_options[key] = value
end
def fetch(key, default_value = nil)
self[key] || default_value
end
def all_of(key)
user = user_options[key]
file = file_options[key]
@ -163,7 +56,7 @@ module Puma
user + file + default
end
def force_defaults
def finalize_values
@default_options.each do |k,v|
if v.respond_to? :call
@default_options[k] = v.call
@ -179,16 +72,18 @@ module Puma
def self.from_file(path)
cfg = new
@dsl._load_from(path)
@file_dsl._load_from(path)
return cfg
end
def initialize(options={}, &blk)
@options = UserFileDefaultOptions.new(options, self.default_options)
@plugins = PluginLoader.new
@dsl = DSL.new(@options.file_options, self)
@options = UserFileDefaultOptions.new(options, self.default_options)
@plugins = PluginLoader.new
@user_dsl = DSL.new(@options.user_options, self)
@file_dsl = DSL.new(@options.file_options, self)
@default_dsl = DSL.new(@options.default_options, self)
if blk
configure(&blk)
@ -198,7 +93,11 @@ module Puma
attr_reader :options, :plugins
def configure(&blk)
@dsl._run(&blk)
blk.call(@user_dsl, @file_dsl, @default_dsl)
ensure
@user_dsl._offer_plugins
@file_dsl._offer_plugins
@default_dsl._offer_plugins
end
def initialize_copy(other)
@ -252,7 +151,7 @@ module Puma
end
files.each do |f|
@dsl.load(f)
@file_dsl.load(f)
end
@options
end
@ -260,7 +159,7 @@ module Puma
# Call once all configuration (included from rackup files)
# is loaded to flesh out any defaults
def clamp
@options.force_defaults
@options.finalize_values
end
# Injects the Configuration object into the env

View file

@ -3,12 +3,12 @@ require 'puma/configuration'
module Puma
def self.run(opts={})
cfg = Puma::Configuration.new do |c|
cfg = Puma::Configuration.new do |user_config|
if port = opts[:port]
c.port port
user_config.port port
end
c.quiet
user_config.quiet
yield c
end

View file

@ -16,7 +16,7 @@ module Puma
end
def initialize(options, config)
@config = config
@config = config
@options = options
@plugins = []
@ -42,12 +42,6 @@ module Puma
@plugins.clear
end
def _run(&blk)
blk.call self
ensure
_offer_plugins
end
def inject(&blk)
instance_eval(&blk)
end

View file

@ -34,9 +34,9 @@ module Puma
#
# Examples:
#
# conf = Puma::Configuration.new do |c|
# c.threads 1, 10
# c.app do |env|
# conf = Puma::Configuration.new do |user_config|
# user_config.threads 1, 10
# user_config.app do |env|
# [200, {}, ["hello world"]]
# end
# end

View file

@ -15,38 +15,38 @@ module Rack
options = DEFAULT_OPTIONS.merge(options)
conf = ::Puma::Configuration.new(options) do |c|
c.quiet
conf = ::Puma::Configuration.new(options) do |user_config, file_config, default_config|
user_config.quiet
if options.delete(:Verbose)
app = Rack::CommonLogger.new(app, STDOUT)
end
if options[:environment]
c.environment options[:environment]
user_config.environment options[:environment]
end
if options[:Threads]
min, max = options.delete(:Threads).split(':', 2)
c.threads min, max
user_config.threads min, max
end
host = options[:Host]
if host && (host[0,1] == '.' || host[0,1] == '/')
c.bind "unix://#{host}"
user_config.bind "unix://#{host}"
elsif host && host =~ /^ssl:\/\//
uri = URI.parse(host)
uri.port ||= options[:Port] || ::Puma::Configuration::DefaultTCPPort
c.bind uri.to_s
user_config.bind uri.to_s
else
host ||= ::Puma::Configuration::DefaultTCPHost
port = options[:Port] || ::Puma::Configuration::DefaultTCPPort
c.port port, host
user_config.port port, host
end
c.app app
user_config.app app
end
conf
end

View file

@ -28,13 +28,12 @@ class TestConfigFile < Minitest::Test
def test_double_bind_port
port = (rand(10_000) + 30_000).to_s
with_env("PORT" => port) do
conf = Puma::Configuration.new do |c|
c.bind "tcp://#{Puma::Configuration::DefaultTCPHost}:#{port}"
c.load "test/config/app.rb"
conf = Puma::Configuration.new do |user_config, file_config, default_config|
user_config.bind "tcp://#{Puma::Configuration::DefaultTCPHost}:#{port}"
file_config.load "test/config/app.rb"
end
conf.load
assert_equal ["tcp://0.0.0.0:#{port}"], conf.options[:binds]
end
end

View file

@ -54,42 +54,42 @@ class TestPathHandler < Minitest::Test
end
end
def test_user_supplied_port_wins_over_config_file
user_port = 5001
file_port = 6001
options = {}
# def test_user_supplied_port_wins_over_config_file
# user_port = 5001
# file_port = 6001
# options = {}
Tempfile.open("puma.rb") do |f|
f.puts "port #{file_port}"
f.close
# Tempfile.open("puma.rb") do |f|
# f.puts "port #{file_port}"
# f.close
options[:config_files] = [f.path]
options[:user_supplied_options] = [:Port]
options[:Port] = user_port
# options[:config_files] = [f.path]
# options[:user_supplied_options] = [:Port]
# options[:Port] = user_port
conf = Rack::Handler::Puma.config(app, options)
conf.load
assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds]
end
end
# conf = Rack::Handler::Puma.config(app, options)
# conf.load
# assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds]
# end
# end
def test_default_port_loses_to_config_file
user_port = 5001
file_port = 6001
options = {}
# def test_default_port_loses_to_config_file
# user_port = 5001
# file_port = 6001
# options = {}
Tempfile.open("puma.rb") do |f|
f.puts "port #{file_port}"
f.close
# Tempfile.open("puma.rb") do |f|
# f.puts "port #{file_port}"
# f.close
options[:config_files] = [f.path]
options[:user_supplied_options] = []
options[:Port] = user_port
# options[:config_files] = [f.path]
# options[:user_supplied_options] = []
# options[:Port] = user_port
conf = Rack::Handler::Puma.config(app, options)
conf.load
puts conf.options[:binds]
assert_equal ["tcp://0.0.0.0:#{file_port}"], conf.options[:binds]
end
end
# conf = Rack::Handler::Puma.config(app, options)
# conf.load
# puts conf.options[:binds]
# assert_equal ["tcp://0.0.0.0:#{file_port}"], conf.options[:binds]
# end
# end
end