2011-12-05 12:01:19 -05:00
|
|
|
module Puma
|
|
|
|
module App
|
|
|
|
class Status
|
2011-12-05 18:58:23 -05:00
|
|
|
def initialize(server, cli)
|
2011-12-05 12:01:19 -05:00
|
|
|
@server = server
|
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
|
|
|
|
|
|
|
|
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']
|
|
|
|
when "/stop"
|
|
|
|
@server.stop
|
2012-04-29 15:18:21 -04:00
|
|
|
return rack_response(200, OK_STATUS)
|
2011-12-05 12:01:19 -05:00
|
|
|
|
|
|
|
when "/halt"
|
|
|
|
@server.halt
|
2012-04-29 15:18:21 -04:00
|
|
|
return rack_response(200, OK_STATUS)
|
2011-12-05 12:01:19 -05:00
|
|
|
|
2011-12-05 18:58:23 -05:00
|
|
|
when "/restart"
|
|
|
|
if @cli and @cli.restart_on_stop!
|
2012-04-04 11:38:22 -04:00
|
|
|
@server.begin_restart
|
|
|
|
|
2012-04-29 15:18:21 -04:00
|
|
|
return rack_response(200, OK_STATUS)
|
2011-12-05 18:58:23 -05:00
|
|
|
else
|
2012-04-29 15:18:21 -04:00
|
|
|
return rack_response(200, '{ "status": "not configured" }')
|
2011-12-05 18:58:23 -05:00
|
|
|
end
|
|
|
|
|
2011-12-05 12:01:19 -05:00
|
|
|
when "/stats"
|
|
|
|
b = @server.backlog
|
|
|
|
r = @server.running
|
2012-04-29 15:18:21 -04:00
|
|
|
return rack_response(200, %Q!{ "backlog": #{b}, "running": #{r} }!)
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
|
2012-04-29 15:24:00 -04:00
|
|
|
rack_response 404, "Unsupported action", 'text/plain'
|
2012-04-29 15:18:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-04-29 15:24:00 -04:00
|
|
|
def rack_response(status, body, content_type='application/json')
|
2012-05-12 19:27:14 -04:00
|
|
|
[status, { 'Content-Type' => content_type, 'Content-Length' => body.bytesize.to_s }, [body]]
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|