Sha256: 910883c56164070e1d47ebc1a2b944d08ebdccb69444c7a143621fc2c50c18e4

Contents?: true

Size: 2 KB

Versions: 4

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

require 'json'

module Puma
  module App
    # Check out {#call}'s source code to see what actions this web application
    # can respond to.
    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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
puma-4.1.1-java lib/puma/app/status.rb
puma-4.1.1 lib/puma/app/status.rb
puma-4.1.0-java lib/puma/app/status.rb
puma-4.1.0 lib/puma/app/status.rb