<commit_before>require 'rack'
require 'listen'

module Flutterby
  class Server
    def initialize(root)
      @root = root
    end

    def run!
      # Set up listener
      listener = Listen.to(@root.fs_path) do |modified, added, removed|
        # puts "modified absolute path: #{modified}"
        # puts "added absolute path: #{added}"
        # puts "removed absolute path: #{removed}"

        puts "Change detected, reloading everything!"
        @root.reload!
      end

      # Set up server
      server = Rack::Handler::WEBrick

      # Make sure we handle interrupts correctly
      trap('INT') do
        listener.stop
        server.shutdown
      end

      # Go!
      listener.start
      server.run self
    end

    def call(env)
      req  = Rack::Request.new(env)
      res = Rack::Response.new([], 200, {})

      parts = req.path.split("/").reject(&:empty?)

      current = @root

      result = catch :halt do
        loop do
          # halt if we're not supposed to serve current entity
          throw :halt, :error_404 unless current.should_publish?

          case current
          when Flutterby::Folder then
            # If no further parts are requested, let's look for an index
            # document and serve that instead.
            if child = current.find(parts.empty? ? "index" : parts.shift)
              current = child
            else
              throw :halt, :error_404
            end
          when Flutterby::File then
            # Determine MIME type
            mime_type = MIME::Types.type_for(current.ext) || "text/plain"

            # Build response
            res.headers["Content-Type"] = mime_type
            res.body = [current.render]
            throw :halt
          end
        end
      end

      case result
      when :error_404 then<commit_msg>Actually serve 404s with 404s :)<commit_after>require 'rack'
require 'listen'

module Flutterby
  class Server
    def initialize(root)
      @root = root
    end

    def run!
      # Set up listener
      listener = Listen.to(@root.fs_path) do |modified, added, removed|
        # puts "modified absolute path: #{modified}"
        # puts "added absolute path: #{added}"
        # puts "removed absolute path: #{removed}"

        puts "Change detected, reloading everything!"
        @root.reload!
      end

      # Set up server
      server = Rack::Handler::WEBrick

      # Make sure we handle interrupts correctly
      trap('INT') do
        listener.stop
        server.shutdown
      end

      # Go!
      listener.start
      server.run self
    end

    def call(env)
      req  = Rack::Request.new(env)
      res = Rack::Response.new([], 200, {})

      parts = req.path.split("/").reject(&:empty?)

      current = @root

      result = catch :halt do
        loop do
          # halt if we're not supposed to serve current entity
          throw :halt, :error_404 unless current.should_publish?

          case current
          when Flutterby::Folder then
            # If no further parts are requested, let's look for an index
            # document and serve that instead.
            if child = current.find(parts.empty? ? "index" : parts.shift)
              current = child
            else
              throw :halt, :error_404
            end
          when Flutterby::File then
            # Determine MIME type
            mime_type = MIME::Types.type_for(current.ext) || "text/plain"

            # Build response
            res.headers["Content-Type"] = mime_type
            res.body = [current.render]
            throw :halt
          end
        end
      end

      case result
      when :error_404 then
        res.status = 404
{'fraction_non_alphanumeric': 0.09061313076505698, 'fraction_numerical': 0.008952794357026587, 'mean_word_length': 1.768018018018018, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1842227', 'n_tokens_mistral': 1037, 'n_tokens_neox': 984, 'n_words': 318}