<commit_before>class Router
  getter :routes
  getter :controllers
  getter :matched_route

  include Support::RoutesPainter
  include Sugar::Klass
  singleton_INSTANCE

  # This macro is a hack that allows to instansiate controllers through 
  # @controllers.fetch("ControllerName")
  macro register_controller(klass_name)
    @controllers["{{klass_name.id}}"] = {{klass_name.id}}
  end

  def initialize()
    @routes      = [] of Dispatch::Route
    @controllers = {} of String => Base::Controller.class 
    @methods     = {} of String => Symbol
    @matched_route = Dispatch::Route.new("/not_found/", "HttpError", "404" )
    @controllers_instances = {} of String => Base::Controller
  end

  # Adds controller to hash @controllers and make it available for app
  def register(controller)
    register_controller controller
  end 

  # It allows to 'draw' routes like you can do in Rails routes.rb
  def draw(&block)
    with self yield
  end

  # Return true if path route exists, and sets @matched_route
  def exists?(path, method)
    exists = false
    @routes.each do |route|
      if route.matches?(path, method)
        exists = true
        @matched_route = route
        break
      end
    end
    return exists
  end

  def process_named_route(request, response)
    controller = @matched_route.controller.capitalize+"Controller"
    controller_instance = @controllers_instances[controller] ||=  @controllers.fetch(controller).new
    controller_instance.set_env(request, response)
    response = @controllers_instances[controller].call_action(@matched_route.action)
  end

  # Actually, performs a routing 
  def call(request : Http::Request) : Http::Response
    response = Http::Response.new(404, "Not found")
    if exists? request.path, request.method
      response = process_named_route(request, response)
    end
    response
  end
end<commit_msg>Add default routing mapped to controller and actions<commit_after>class Router
  getter :routes
  getter :controllers
  getter :matched_route

  include Support::RoutesPainter
  include Sugar::Klass
  singleton_INSTANCE

  # This macro is a hack that allows to instansiate controllers through 
  # @controllers.fetch("ControllerName")
  macro register_controller(klass_name)
    @controllers["{{klass_name.id}}"] = {{klass_name.id}}
  end

  def initialize()
    @routes      = [] of Dispatch::Route
    @controllers = {} of String => Base::Controller.class 
    @methods     = {} of String => Symbol
    @matched_route = Dispatch::Route.new("/not_found/", "HttpError", "404" )
    @controllers_instances = {} of String => Base::Controller
  end

  # Adds controller to hash @controllers and make it available for app
  def register(controller)
    register_controller controller
  end 

  # It allows to 'draw' routes like you can do in Rails routes.rb
  def draw(&block)
    with self yield
  end

  # Return true if path route exists, and sets @matched_route
  def exists?(path, method)
    exists = false
    @routes.each do |route|
      if route.matches?(path, method)
        exists = true
        @matched_route = route
        break
      end
    end
    return exists
  end

  def process_named_route(request : Http::Request, response : Http::Response)
    controller = @matched_route.controller.capitalize+"Controller"
    controller_instance = @controllers_instances[controller] ||=  @controllers.fetch(controller).new
    controller_instance.set_env(request, response)
    response = @controllers_instances[controller].call_action(@matched_route.action)
  end

  def process_default_route(request : Http::Request, response : Http::Response)
    regexp = "^(?<controller>[a-z]*)\/(?<action>[a-z]*)"
    match = Regex.new(regexp).match("request.path")
    if match
      controller = match["controller"] as String
      action     = match["action"]     as String
      if @controllers.has_key? controller
        controller = controller.capitalize+"Controller"
        controller_instance = @controllers_instances[controller] ||=  @controllers.fetch(controller).new
        controller_instance.set_env(request, response)
      end
      response = @controllers_instances[controller].call_action(action)
    else
      response
    end
  end



  # Actually, performs a routing 
  def call(request : Http::Request) : Http::Response
    response = Http::Response.new(404, "Not found")
    if exists? request.path, request.method
      response = process_named_route(request, response)
    else
      response = process_default_route(request, response)
    end
  end
end
{'fraction_non_alphanumeric': 0.09848318311716861, 'fraction_numerical': 0.002637942404924159, 'mean_word_length': 3.676258992805755, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '498595', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1289, 'n_words': 351}