Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 49 lines (41 sloc) 1.164 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
module Puma
  module App
    class Status
      def initialize(server, cli)
        @server = server
        @cli = cli
        @auth_token = nil
      end

      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 [403, {}, ["Invalid auth token"]]
        end

        case env['PATH_INFO']
        when "/stop"
          @server.stop
          return [200, {}, ['{ "status": "ok" }']]

        when "/halt"
          @server.halt
          return [200, {}, ['{ "status": "ok" }']]

        when "/restart"
          if @cli and @cli.restart_on_stop!
            @server.stop
            return [200, {}, ['{ "status": "ok" }']]
          else
            return [200, {}, ['{ "status": "not configured" }']]
          end

        when "/stats"
          b = @server.backlog
          r = @server.running
          return [200, {}, ["{ \"backlog\": #{b}, \"running\": #{r} }"]]
        end

        [404, {}, ["Unsupported action"]]
      end
    end
  end
end
Something went wrong with that request. Please try again.