Stash
This commit is contained in:
parent
89e2b043ce
commit
dcc753a106
|
@ -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,14 +23,10 @@ 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 #{request}")
|
||||
|
||||
Logger.info("Building body.")
|
||||
Logger.info("Building response body.")
|
||||
|
||||
# NOTE: Reliability testing.
|
||||
if String.match?(request, ~r{GET /error}) do
|
||||
raise(request)
|
||||
end
|
||||
|
||||
# TODO: Handle routing here.
|
||||
a = String.split(request, "\n\r")
|
||||
|
@ -67,6 +63,8 @@ defmodule RlRepo.ClientHandler do
|
|||
_ ->
|
||||
RlRepo.Router.status_404()
|
||||
end
|
||||
|
||||
ret
|
||||
end
|
||||
|
||||
def create_return_code_string(return_code) do
|
||||
|
@ -78,9 +76,10 @@ defmodule RlRepo.ClientHandler do
|
|||
|
||||
def create_response_header(body) do
|
||||
{return_code, content_type, body} = body
|
||||
html_ver = "1.1"
|
||||
|
||||
"""
|
||||
HTTP/1.1 #{create_return_code_string(return_code)}\r
|
||||
HTTP/#{html_ver} #{create_return_code_string(return_code)}\r
|
||||
Content-Type: #{content_type}\r
|
||||
Content-Length: #{byte_size(body)}\r
|
||||
\r
|
||||
|
|
|
@ -5,6 +5,7 @@ defmodule RlRepo do
|
|||
"""
|
||||
|
||||
@doc """
|
||||
Spin up the entire stack.
|
||||
"""
|
||||
def start(port) do
|
||||
{:ok, listener_socket} =
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule RlRepo.Router do
|
|||
require Logger
|
||||
|
||||
@moduledoc """
|
||||
|
||||
TODO: new_route("/<pkg>/", fn -> IO>puts("hello") end)
|
||||
"""
|
||||
def status_404() do
|
||||
placeholders = %{"reason" => "Unknown"}
|
||||
|
@ -18,12 +18,12 @@ defmodule RlRepo.Router do
|
|||
if path == "/favicon.ico" do
|
||||
{200, "afdssadf", "1112"}
|
||||
end
|
||||
|
||||
placeholders = %{"reason" => "Unknown"}
|
||||
|
||||
{200, "text/html", RlRepo.Template.template("button-page", placeholders)}
|
||||
end
|
||||
|
||||
|
||||
def route_3(path, request_type) do
|
||||
Logger.info("#{request_type}")
|
||||
{repo_name, pkg_name, action} = path
|
||||
|
|
|
@ -4,36 +4,19 @@ defmodule RlRepo.Template do
|
|||
def template(template_name, replacements) do
|
||||
Logger.info("Loading template #{template_name}")
|
||||
{:ok, body} = File.read("assets/html/#{template_name}.html")
|
||||
# TODO: Check if there is any actual placeholders
|
||||
v = String.contains?(body, "~(")
|
||||
Logger.info v
|
||||
# if v do
|
||||
len = body |> String.split("~(") |> length()
|
||||
Logger.info body
|
||||
# TODO: Math this out to be equal to the number of splits
|
||||
# if len >= 2 do
|
||||
template_step(replacements, body, len)
|
||||
# Logger.info "Len greater 2"
|
||||
# end
|
||||
# end
|
||||
# body
|
||||
|
||||
inner_template(body, replacements)
|
||||
end
|
||||
# TODO: remodel this into inner_template so the test can call it directly and so can template
|
||||
def test_template(template_name, replacements) do
|
||||
body = "~(reason)"
|
||||
|
||||
def inner_template(body, replacements) do
|
||||
# TODO: Check if there is any actual placeholders
|
||||
v = String.contains?(body, "~(")
|
||||
Logger.info v
|
||||
# if v do
|
||||
len = body |> String.split("~(") |> length()
|
||||
Logger.info body
|
||||
# TODO: Math this out to be equal to the number of splits
|
||||
# if len >= 2 do
|
||||
template_step(replacements, body, len)
|
||||
# Logger.info "Len greater 2"
|
||||
# end
|
||||
# end
|
||||
# body
|
||||
# Logger.info(v)
|
||||
|
||||
len = body |> String.split("~(") |> length()
|
||||
# Logger.info(body)
|
||||
|
||||
template_step(replacements, body, len)
|
||||
end
|
||||
|
||||
def template_step(replacements, body, steps_left) do
|
||||
|
|
|
@ -4,14 +4,18 @@ defmodule RlRepoTest do
|
|||
doctest RlRepo
|
||||
|
||||
test "single template replace" do
|
||||
string = "~(replace)"
|
||||
body = "~(replace)"
|
||||
placeholders = %{"replace" => "DONE"}
|
||||
|
||||
|
||||
|
||||
b = RlRepo.Template.test_template(string, placeholders)
|
||||
|
||||
|
||||
assert :world == :world
|
||||
assert RlRepo.Template.inner_template(body, placeholders) == "DONE"
|
||||
end
|
||||
|
||||
|
||||
test "multi template replace" do
|
||||
body = "~(replace)abc~(replace)"
|
||||
placeholders = %{"replace" => "DONE"}
|
||||
|
||||
assert RlRepo.Template.inner_template(body, placeholders) == "DONEabcDONE"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue