class Puma::Stats::App

Public Class Methods

new(launcher) click to toggle source
# File lib/puma/stats/app.rb, line 8
def initialize(launcher)
  @launcher = launcher
  @auth_token = launcher.options[:stats_token]
end

Public Instance Methods

call(env) click to toggle source
# File lib/puma/stats/app.rb, line 13
def call(env)
  return rack_response(403, 'Invalid stats auth token', 'text/plain') unless authenticate(env)

  case env['PATH_INFO']
  when %r{/puma-stats-gc$}
    rack_response(200, GC.stat.to_json)

  when %r{/puma-stats$}
    rack_response(200, @launcher.stats)

  else
    rack_response 404, 'Unsupported request', 'text/plain'
  end
end

Private Instance Methods

authenticate(env) click to toggle source
# File lib/puma/stats/app.rb, line 30
def authenticate(env)
  return true unless @auth_token

  env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
end
rack_response(status, body, content_type = 'application/json') click to toggle source
# File lib/puma/stats/app.rb, line 36
def rack_response(status, body, content_type = 'application/json')
  headers = {
    'Content-Type' => content_type,
    'Content-Length' => body.bytesize.to_s
  }
  [status, headers, [body]]
end