1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/puma/app/status.rb
jc00ke ede98dadfd Return valid Rack responses from status server
Use Rack::Lint to check the response is valid. Existing code really just
needed Content-Length & Content-Type headers.
2012-04-29 12:18:21 -07:00

56 lines
1.4 KiB
Ruby

module Puma
module App
class Status
def initialize(server, cli)
@server = server
@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 call(env)
unless authenticate(env)
return rack_response(403, "Invalid auth token")
end
case env['PATH_INFO']
when "/stop"
@server.stop
return rack_response(200, OK_STATUS)
when "/halt"
@server.halt
return rack_response(200, OK_STATUS)
when "/restart"
if @cli and @cli.restart_on_stop!
@server.begin_restart
return rack_response(200, OK_STATUS)
else
return rack_response(200, '{ "status": "not configured" }')
end
when "/stats"
b = @server.backlog
r = @server.running
return rack_response(200, %Q!{ "backlog": #{b}, "running": #{r} }!)
end
rack_response 404, "Unsupported action"
end
private
def rack_response(status, body)
[status, { 'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]]
end
end
end
end