/
status.rb
77 lines (62 loc) · 1.9 KB
/
status.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
70
71
72
73
74
75
76
77
# frozen_string_literal: true
require 'json'
module Puma
module App
class Status
def initialize(cli)
@cli = cli
@auth_token = nil
end
OK_STATUS = '{ "status": "ok" }'.freeze
attr_accessor :auth_token
def authenticate(env)
return true unless @auth_token
env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
end
def rack_response(status, body, content_type='application/json')
headers = {
'Content-Type' => content_type,
'Content-Length' => body.bytesize.to_s
}
[status, headers, [body]]
end
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 /\/reload-worker-directory$/
if !@cli.send(:reload_worker_directory)
return rack_response(404, '{ "error": "reload_worker_directory not available" }')
else
return rack_response(200, OK_STATUS)
end
when /\/gc$/
GC.start
return rack_response(200, OK_STATUS)
when /\/gc-stats$/
return rack_response(200, GC.stat.to_json)
when /\/stats$/
return rack_response(200, @cli.stats)
else
rack_response 404, "Unsupported action", 'text/plain'
end
end
end
end
end