<commit_before>require "http/server"

module Playground
  module HttpServer
    extend self
    
    class Handler
      class ClientError < Exception; end
      
      UNPROCESSABLE_ENTITY = 422
      
      def initialize(
        @request : HTTP::Request,
        @response : HTTP::Server::Response,
        @fiber = Fiber
      )
      end
      
      private getter request, response
      
      def call
        t1 = Time.now
        response.content_type = "text/plain"
        @fiber.sleep requested_sleep_time_ms / 1000
        t2 = Time.now
        response.print "I'm done! Started at: #{t1}, ended at #{t2}"
      rescue err : ClientError
        response.respond_with_error err, UNPROCESSABLE_ENTITY
      end
      
      private def requested_sleep_time_ms
        request.resource.lchop('/').to_i
      rescue ArgumentError
        raise ClientError.new "Path must simply by an integer, ms to sleep"
      end
    end
    
    def listen
      handlers = [
        HTTP::ErrorHandler.new,
        HTTP::LogHandler.new
      ]
      
      server = HTTP::Server.new(8080, handlers) do |context|
        handler = Handler.new context.request, context.response
        handler.call()
      end
      
      Logger.instance.info "Listening on http://127.0.0.1:8080"
      server.listen
    end
  end
end<commit_msg>Playing around with HTTP handlers.<commit_after>require "http/server"

module Playground
  module HttpServer
    extend self
    
    class SleepyRequestHandler
      include HTTP::Handler
      
      class ClientError < Exception; end
      
      UNPROCESSABLE_ENTITY = 422
      
      def initialize(@fiber = Fiber)
      end
      
      def call(context)
        t1 = Time.now
        context.response.content_type = "text/plain"
        @fiber.sleep requested_sleep_time_ms(context.request) / 1000
        t2 = Time.now
        context.response.print "I'm done! Started at: #{t1}, ended at #{t2}"
      rescue err : ClientError
        context.response.respond_with_error err, UNPROCESSABLE_ENTITY
      end
      
      private def requested_sleep_time_ms(request)
        request.resource.lchop('/').to_i
      rescue ArgumentError
        raise ClientError.new "Path must simply by an integer, ms to sleep"
      end
    end
    
    def listen
      handlers = [
        HTTP::ErrorHandler.new,
        HTTP::LogHandler.new,
        SleepyRequestHandler.new
      ]
      
      Logger.instance.info "Listening on http://127.0.0.1:8080"
      server = HTTP::Server.new(8080, handlers).listen
    end
  end
end
{'fraction_non_alphanumeric': 0.0792156862745098, 'fraction_numerical': 0.0196078431372549, 'mean_word_length': 2.0332936979785967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6249073', 'n_tokens_mistral': 772, 'n_tokens_neox': 735, 'n_words': 186}