Class: Puma::Configuration

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

Defined Under Namespace

Classes: ConfigMiddleware, DSL

Constant Summary

DefaultRackup =
"config.ru"
DefaultTCPHost =
"0.0.0.0"
DefaultTCPPort =
9292
DefaultWorkerTimeout =
60
DefaultWorkerShutdownTimeout =
30

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puma/configuration.rb', line 20

def initialize(options)
  @options = options
  @options[:mode] ||= :http
  @options[:binds] ||= []
  @options[:on_restart] ||= []
  @options[:before_worker_shutdown] ||= []
  @options[:before_worker_boot] ||= []
  @options[:after_worker_boot] ||= []
  @options[:worker_timeout] ||= DefaultWorkerTimeout
  @options[:worker_shutdown_timeout] ||= DefaultWorkerShutdownTimeout
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



32
33
34
# File 'lib/puma/configuration.rb', line 32

def options
  @options
end

Class Method Details

.temp_pathObject



166
167
168
169
170
171
# File 'lib/puma/configuration.rb', line 166

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 options from the rackup file, and set @app.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/puma/configuration.rb', line 101

def app
  app = @options[:app]

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

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

    config_ru_binds = []

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

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

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

    logger = @options[:logger] || STDOUT
    return TCPLogger.new(logger, app, @options[:quiet])
  end

  if !@options[:quiet] and @options[:environment] == "development"
    logger = @options[:logger] || STDOUT
    app = Rack::CommonLogger.new(app, logger)
  end

  return ConfigMiddleware.new(self, app)
end

#app_configured?Boolean

Indicate if there is a properly configured app

Returns:

  • (Boolean)


90
91
92
# File 'lib/puma/configuration.rb', line 90

def app_configured?
  @options[:app] || File.exist?(rackup)
end

#infer_tagObject



71
72
73
# File 'lib/puma/configuration.rb', line 71

def infer_tag
  File.basename Dir.getwd
end

#initialize_copy(other) ⇒ Object



34
35
36
# File 'lib/puma/configuration.rb', line 34

def initialize_copy(other)
  @options = @options.dup
end

#loadObject



38
39
40
41
42
43
44
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
# File 'lib/puma/configuration.rb', line 38

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

  unless @options[:tag]
    @options[:tag] = infer_tag
  end

  @options[:binds].uniq!
end

#rackupObject



94
95
96
# File 'lib/puma/configuration.rb', line 94

def rackup
  @options[:rackup] || DefaultRackup
end

#setup_random_tokenObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puma/configuration.rb', line 138

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.exist?("/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