Class: Puma::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/configuration.rb

Defined Under Namespace

Classes: DSL

Constant Summary collapse

DefaultRackup =
"config.ru"
DefaultTCPHost =
"0.0.0.0"
DefaultTCPPort =
9292

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration.



8
9
10
11
# File 'lib/puma/configuration.rb', line 8

def initialize(options)
  @options = options
  @options[:binds] ||= []
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/puma/configuration.rb', line 13

def options
  @options
end

Class Method Details

.temp_pathObject



100
101
102
103
104
105
# File 'lib/puma/configuration.rb', line 100

def self.temp_path
  require 'tmpdir'

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

Instance Method Details

#appObject

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/puma/configuration.rb', line 45

def app
  if app = @options[:app]
    return app
  end

  path = @options[:rackup] || DefaultRackup

  unless File.exists?(path)
    raise "Missing rackup file '#{path}'"
  end

  app, options = Rack::Builder.parse_file path
  @options.merge! options

  options.each do |key,val|
    if key.to_s[0,4] == "bind"
      @options[:binds] << val
    end
  end

  unless @options[:quiet]
    app = Rack::CommonLogger.new(app, STDOUT)
  end

  return app
end

#loadObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puma/configuration.rb', line 15

def load
  if path = @options[:config_file]
    DSL.new(@options)._load_from path
  end

  # Rakeup default option support
  if host = @options[:Host]
    port = @options[:Port] || DefaultTCPPort

    @options[:binds] << "tcp://#{host}:#{port}"
  end

  if @options[:binds].empty?
    @options[:binds] << "tcp://#{DefaultTCPHost}:#{DefaultTCPPort}"
  end

  if @options[:control_url] == "auto"
    path = Configuration.temp_path
    @options[:control_url] = "unix://#{path}"
    @options[:control_url_temp] = path
  end

  unless @options[:control_auth_token]
    setup_random_token
  end
end

#setup_random_tokenObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/puma/configuration.rb', line 72

def setup_random_token
  begin
    require 'openssl'
  rescue LoadError
  end

  count = 16

  bytes = nil

  if defined? OpenSSL::Random
    bytes = OpenSSL::Random.random_bytes(count)
  elsif File.exists?("/dev/urandom")
    File.open("/dev/urandom") do |f|
      bytes = f.read(count)
    end
  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

  @options[:control_auth_token] = token
end