2011-12-05 12:01:19 -05:00
|
|
|
module Puma
|
|
|
|
module App
|
|
|
|
class Status
|
2013-07-05 19:08:13 -04:00
|
|
|
def initialize(cli)
|
2011-12-05 18:58:23 -05:00
|
|
|
@cli = cli
|
2011-12-06 19:22:02 -05:00
|
|
|
@auth_token = nil
|
|
|
|
end
|
2012-04-29 15:18:21 -04:00
|
|
|
OK_STATUS = '{ "status": "ok" }'.freeze
|
2011-12-06 19:22:02 -05:00
|
|
|
|
|
|
|
attr_accessor :auth_token
|
|
|
|
|
|
|
|
def authenticate(env)
|
|
|
|
return true unless @auth_token
|
|
|
|
env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
|
2013-07-05 19:08:13 -04:00
|
|
|
def rack_response(status, body, content_type='application/json')
|
|
|
|
headers = {
|
|
|
|
'Content-Type' => content_type,
|
|
|
|
'Content-Length' => body.bytesize.to_s
|
|
|
|
}
|
|
|
|
|
|
|
|
[status, headers, [body]]
|
|
|
|
end
|
|
|
|
|
2011-12-05 12:01:19 -05:00
|
|
|
def call(env)
|
2011-12-06 19:22:02 -05:00
|
|
|
unless authenticate(env)
|
2012-04-29 15:24:00 -04:00
|
|
|
return rack_response(403, 'Invalid auth token', 'text/plain')
|
2011-12-06 19:22:02 -05:00
|
|
|
end
|
|
|
|
|
2011-12-05 12:01:19 -05:00
|
|
|
case env['PATH_INFO']
|
2012-10-03 21:16:31 -04:00
|
|
|
when /\/stop$/
|
2013-07-05 19:08:13 -04:00
|
|
|
@cli.stop
|
2012-04-29 15:18:21 -04:00
|
|
|
return rack_response(200, OK_STATUS)
|
2011-12-05 12:01:19 -05:00
|
|
|
|
2012-10-03 21:16:31 -04:00
|
|
|
when /\/halt$/
|
2013-07-05 19:08:13 -04:00
|
|
|
@cli.halt
|
2012-04-29 15:18:21 -04:00
|
|
|
return rack_response(200, OK_STATUS)
|
2011-12-05 12:01:19 -05:00
|
|
|
|
2012-10-03 21:16:31 -04:00
|
|
|
when /\/restart$/
|
2013-07-05 19:08:13 -04:00
|
|
|
@cli.restart
|
|
|
|
return rack_response(200, OK_STATUS)
|
2012-04-04 11:38:22 -04:00
|
|
|
|
2013-07-05 19:08:13 -04:00
|
|
|
when /\/phased-restart$/
|
|
|
|
if !@cli.phased_restart
|
2013-08-06 15:47:40 -04:00
|
|
|
return rack_response(404, '{ "error": "phased restart not available" }')
|
2011-12-05 18:58:23 -05:00
|
|
|
else
|
2013-07-05 19:08:13 -04:00
|
|
|
return rack_response(200, OK_STATUS)
|
2011-12-05 18:58:23 -05:00
|
|
|
end
|
|
|
|
|
2014-02-25 08:52:20 -05:00
|
|
|
when /\/reload-worker-directory$/
|
|
|
|
if !@cli.reload_worker_directory
|
|
|
|
return rack_response(404, '{ "error": "reload_worker_directory not available" }')
|
|
|
|
else
|
|
|
|
return rack_response(200, OK_STATUS)
|
|
|
|
end
|
|
|
|
|
2012-10-03 21:16:31 -04:00
|
|
|
when /\/stats$/
|
2013-07-05 19:08:13 -04:00
|
|
|
return rack_response(200, @cli.stats)
|
|
|
|
else
|
|
|
|
rack_response 404, "Unsupported action", 'text/plain'
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|