Class: Puma::App::Status

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

Constant Summary collapse

OK_STATUS =
'{ "status": "ok" }'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli) ⇒ Status

Returns a new instance of Status.



4
5
6
7
# File 'lib/puma/app/status.rb', line 4

def initialize(cli)
  @cli = cli
  @auth_token = nil
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



10
11
12
# File 'lib/puma/app/status.rb', line 10

def auth_token
  @auth_token
end

Instance Method Details

#authenticate(env) ⇒ Object



12
13
14
15
# File 'lib/puma/app/status.rb', line 12

def authenticate(env)
  return true unless @auth_token
  env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
end

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puma/app/status.rb', line 26

def call(env)
  unless authenticate(env)
    return rack_response(403, 'Invalid auth token', 'text/plain')
  end

  case env['PATH_INFO']
  when /\/stop$/
    @cli.stop
    return rack_response(200, OK_STATUS)

  when /\/halt$/
    @cli.halt
    return rack_response(200, OK_STATUS)

  when /\/restart$/
    @cli.restart
    return rack_response(200, OK_STATUS)

  when /\/phased-restart$/
    if !@cli.phased_restart
      return rack_response(404, '{ "error": "phased restart not available" }')
    else
      return rack_response(200, OK_STATUS)
    end

  when /\/stats$/
    return rack_response(200, @cli.stats)
  else
    rack_response 404, "Unsupported action", 'text/plain'
  end
end

#rack_response(status, body, content_type = 'application/json') ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/puma/app/status.rb', line 17

def rack_response(status, body, content_type='application/json')
  headers = {
    'Content-Type' => content_type,
    'Content-Length' => body.bytesize.to_s
  }

  [status, headers, [body]]
end