class Puma::Configuration

Attributes

options[R]
plugins[R]

Public Class Methods

from_file(path) click to toggle source
# File lib/puma/configuration.rb, line 127
def self.from_file(path)
  cfg = new

  DSL.new(cfg.options, cfg)._load_from path

  return cfg
end
new(options={}, &blk) click to toggle source
# File lib/puma/configuration.rb, line 135
def initialize(options={}, &blk)
  @options = LeveledOptions.new(default_options, options)

  @plugins = PluginLoader.new

  if blk
    configure(&blk)
  end
end
temp_path() click to toggle source
# File lib/puma/configuration.rb, line 274
def self.temp_path
  require 'tmpdir'

  t = (Time.now.to_f * 1000).to_i
  "#{Dir.tmpdir}/puma-status-#{t}-#{$$}"
end

Private Class Methods

random_token() click to toggle source
# File lib/puma/configuration.rb, line 328
def self.random_token
  begin
    require 'openssl'
  rescue LoadError
  end

  count = 16

  bytes = nil

  if defined? OpenSSL::Random
    bytes = OpenSSL::Random.random_bytes(count)
  elsif File.exist?("/dev/urandom")
    File.open('/dev/urandom') { |f| bytes = f.read(count) }
  end

  if bytes
    token = ""
    bytes.each_byte { |b| token << b.to_s(16) }
  else
    token = (0..count).to_a.map { rand(255).to_s(16) }.join
  end

  return token
end

Public Instance Methods

app() click to toggle source

Load the specified rackup file, pull options from the rackup file, and set @app.

# File lib/puma/configuration.rb, line 242
def app
  found = options[:app] || load_rackup

  if @options[:mode] == :tcp
    require 'puma/tcp_logger'

    logger = @options[:logger]
    quiet = !@options[:log_requests]
    return TCPLogger.new(logger, found, quiet)
  end

  if @options[:log_requests]
    logger = @options[:logger]
    found = CommonLogger.new(found, logger)
  end

  ConfigMiddleware.new(self, found)
end
app_configured?() click to toggle source

Indicate if there is a properly configured app

# File lib/puma/configuration.rb, line 231
def app_configured?
  @options[:app] || File.exist?(rackup)
end
clamp() click to toggle source

Call once all configuration (included from rackup files) is loaded to flesh out any defaults

# File lib/puma/configuration.rb, line 211
def clamp
  @options.shift
  @options.force_defaults
end
configure(&blk) click to toggle source
# File lib/puma/configuration.rb, line 147
def configure(&blk)
  @options.shift
  DSL.new(@options, self)._run(&blk)
end
default_options() click to toggle source
# File lib/puma/configuration.rb, line 167
def default_options
  {
    :min_threads => 0,
    :max_threads => 16,
    :log_requests => false,
    :debug => false,
    :binds => ["tcp://#{DefaultTCPHost}:#{DefaultTCPPort}"],
    :workers => 0,
    :daemon => false,
    :mode => :http,
    :worker_timeout => DefaultWorkerTimeout,
    :worker_boot_timeout => DefaultWorkerTimeout,
    :worker_shutdown_timeout => DefaultWorkerShutdownTimeout,
    :remote_address => :socket,
    :tag => method(:infer_tag),
    :environment => lambda { ENV['RACK_ENV'] || "development" },
    :rackup => DefaultRackup,
    :logger => STDOUT,
    :persistent_timeout => Const::PERSISTENT_TIMEOUT
  }
end
environment() click to toggle source

Return which environment we're running in

# File lib/puma/configuration.rb, line 262
def environment
  @options[:environment]
end
flatten() click to toggle source
# File lib/puma/configuration.rb, line 158
def flatten
  dup.flatten!
end
flatten!() click to toggle source
# File lib/puma/configuration.rb, line 162
def flatten!
  @options = @options.flatten
  self
end
initialize_copy(other) click to toggle source
# File lib/puma/configuration.rb, line 152
def initialize_copy(other)
  @conf = nil
  @cli_options = nil
  @options = @options.dup
end
load() click to toggle source
# File lib/puma/configuration.rb, line 189
def load
  files = @options.all_of(:config_files)

  if files.empty?
    imp = %W(config/puma/#{@options[:environment]}.rb config/puma.rb).find { |f|
      File.exist?(f)
    }

    files << imp
  elsif files == ["-"]
    files = []
  end

  files.each do |f|
    @options.shift

    DSL.load @options, self, f
  end
end
load_plugin(name) click to toggle source
# File lib/puma/configuration.rb, line 266
def load_plugin(name)
  @plugins.create name
end
rackup() click to toggle source
# File lib/puma/configuration.rb, line 235
def rackup
  @options[:rackup]
end
run_hooks(key, arg) click to toggle source
# File lib/puma/configuration.rb, line 270
def run_hooks(key, arg)
  @options.all_of(key).each { |b| b.call arg }
end

Private Instance Methods

infer_tag() click to toggle source
# File lib/puma/configuration.rb, line 283
def infer_tag
  File.basename(Dir.getwd)
end
load_rackup() click to toggle source
# File lib/puma/configuration.rb, line 310
def load_rackup
  raise "Missing rackup file '#{rackup}'" unless File.exist?(rackup)

  @options.shift

  rack_app, rack_options = rack_builder.parse_file(rackup)
  @options.merge!(rack_options)

  config_ru_binds = []
  rack_options.each do |k, v|
    config_ru_binds << v if k.to_s.start_with?("bind")
  end

  @options[:binds] = config_ru_binds unless config_ru_binds.empty?

  rack_app
end
rack_builder() click to toggle source

Load and use the normal Rack builder if we can, otherwise fallback to our minimal version.

# File lib/puma/configuration.rb, line 289
def rack_builder
  # Load bundler now if we can so that we can pickup rack from
  # a Gemfile
  if ENV.key? 'PUMA_BUNDLER_PRUNED'
    begin
      require 'bundler/setup'
    rescue LoadError
    end
  end

  begin
    require 'rack'
    require 'rack/builder'
  rescue LoadError
    # ok, use builtin version
    return Puma::Rack::Builder
  else
    return ::Rack::Builder
  end
end