From 65d28b28d43fb2f8135e6d1a84db54d654194fd5 Mon Sep 17 00:00:00 2001
From: Able <abl3theabove@gmail.com>
Date: Fri, 21 Feb 2025 09:22:09 -0600
Subject: [PATCH] MISC: Varios changes

---
 assets/repos.txt      |  2 ++
 lib/client_handler.ex | 69 +++++++++++++++++++++++--------------------
 lib/json.ex           |  6 ++--
 lib/routing.ex        |  4 +--
 lib/templating.ex     |  2 +-
 5 files changed, 46 insertions(+), 37 deletions(-)
 create mode 100644 assets/repos.txt

diff --git a/assets/repos.txt b/assets/repos.txt
new file mode 100644
index 0000000..54365b6
--- /dev/null
+++ b/assets/repos.txt
@@ -0,0 +1,2 @@
+core
+core/kernels
\ No newline at end of file
diff --git a/lib/client_handler.ex b/lib/client_handler.ex
index fe80f21..fd9ad88 100644
--- a/lib/client_handler.ex
+++ b/lib/client_handler.ex
@@ -8,10 +8,10 @@ defmodule RlRepo.ClientHandler do
     Logger.info("Processing client request.")
 
     client_socket
-    |> read_request
-    |> create_response()
-    |> create_response_header()
-    |> write_response(client_socket)
+      |> read_request
+      |> create_response()
+      |> create_response_header()
+      |> write_response(client_socket)
   end
 
   def read_request(client_socket) do
@@ -23,38 +23,44 @@ defmodule RlRepo.ClientHandler do
 
   def create_response(request) do
     # TODO: Split off the info I need from the request body and I guess pass it around also
-    Logger.info("REQUEST BODY #{request}")
+    Logger.info("REQUEST BODY\n#{request}")
+
     Logger.info("Building response body.")
 
-    [get_line | rest] = String.split(request, "\n\r")
+
+    a = String.split(request, "\n\r")
+    [get_line | _rest] = a
+    Logger.info "get-line #{get_line}"
+
 
     b = String.split(get_line, " ")
     [request_type | rest] = b
-    [path | rest] = rest
-    Logger.info("#{request_type} #{path}")
+    Logger.info "REQ_TYPE #{request_type}"
+    [path | _rest] = rest
+    Logger.info("PATH #{path}")
 
-    # Parse path ignoring leading and trailing slash
+    # Parse path
     split_path = String.split(path, "/", trim: true)
-
     split_path_length = length(split_path)
 
-    case split_path_length do
-      1 ->
-        RlRepo.Router.parse_1_segment_path(rest)
-        |> RlRepo.Router.route_1(request_type)
+    # TODO: Handle routing here.
+      case split_path_length do
+        1 ->
+          RlRepo.Router.parse_1_segment_path(split_path)
+          |> RlRepo.Router.route_1(request_type)
 
-      3 ->
-        RlRepo.Router.parse_3_segment_path(rest)
-        |> RlRepo.Router.route_3(request_type)
+        3 ->
+          RlRepo.Router.parse_3_segment_path(split_path)
+          |> RlRepo.Router.route_3(request_type)
 
-      4 ->
-        RlRepo.Router.parse_4_segment_path(rest)
-        |> RlRepo.Router.route_4(request_type)
+        4 ->
+          RlRepo.Router.parse_4_segment_path(split_path)
+          |> RlRepo.Router.route_4(request_type)
 
-      # Note: Error handling.
-      _ ->
-        RlRepo.Router.status_404()
-    end
+        # Note: Error handling.
+        _ ->
+          RlRepo.Router.status_404()
+      end
   end
 
   def create_return_code_string(return_code) do
@@ -67,14 +73,13 @@ defmodule RlRepo.ClientHandler do
   def create_response_header(body) do
     {return_code, content_type, body} = body
     html_ver = "1.1"
-
-    """
-    HTTP/#{html_ver} #{create_return_code_string(return_code)}\r
-    Content-Type: #{content_type}\r
-    Content-Length: #{byte_size(body)}\r
-    \r
-    #{body}
-    """
+"""
+HTTP/#{html_ver} #{create_return_code_string(return_code)}\r
+Content-Type: #{content_type}\r
+Content-Length: #{byte_size(body)}\r
+\r
+#{body}
+"""
   end
 
   def write_response(response, client_socket) do
diff --git a/lib/json.ex b/lib/json.ex
index 9c0b4f3..82deca0 100644
--- a/lib/json.ex
+++ b/lib/json.ex
@@ -3,7 +3,9 @@ defmodule RlRepo.Json do
 
   @moduledoc """
   """
-  def fmt_json(a) do
-    "{\"pkg_names\": 10}"
+  def fmt_as_json(a) do
+    "{\"pkg_name\": \"abc\"," <>
+    "\"" <>
+    "}"
   end
 end
diff --git a/lib/routing.ex b/lib/routing.ex
index 6447157..4b98c05 100644
--- a/lib/routing.ex
+++ b/lib/routing.ex
@@ -37,8 +37,8 @@ defmodule RlRepo.Router do
 
   def route_4(path, request_type) do
     Logger.info("#{request_type}")
-    {repo_name, sub_repo_name, pkg_name, action} = path
-    {200, "text/html", "<p>Hi</p>"}
+    # {repo_name, sub_repo_name, pkg_name, action} = path
+    {200, "application/json", RlRepo.Json.fmt_as_json()}
   end
 
   def parse_1_segment_path(path) do
diff --git a/lib/templating.ex b/lib/templating.ex
index d9a68e4..2074a6e 100644
--- a/lib/templating.ex
+++ b/lib/templating.ex
@@ -16,7 +16,7 @@ defmodule RlRepo.Template do
   end
 
   def template_step(replacements, body, steps_left) do
-    [before_placeholder | rest] = split_path = String.split(body, "~(", parts: 2)
+    [before_placeholder | rest] = String.split(body, "~(", parts: 2)
     [remains | _] = rest
     [placeholder_name | after_placeholder] = String.split(remains, ")", parts: 2)
     [after_placeholder | _] = after_placeholder