REPO: cleanup work
This commit is contained in:
parent
65d28b28d4
commit
c717faa947
assets/html
lib
9
assets/html/repo.html
Normal file
9
assets/html/repo.html
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Repo- ~(repo_name)</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -8,10 +8,10 @@ defmodule RlRepo.ClientHandler do
|
||||||
Logger.info("Processing client request.")
|
Logger.info("Processing client request.")
|
||||||
|
|
||||||
client_socket
|
client_socket
|
||||||
|> read_request
|
|> read_request
|
||||||
|> create_response()
|
|> create_response()
|
||||||
|> create_response_header()
|
|> create_response_header()
|
||||||
|> write_response(client_socket)
|
|> write_response(client_socket)
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_request(client_socket) do
|
def read_request(client_socket) do
|
||||||
|
@ -27,15 +27,13 @@ defmodule RlRepo.ClientHandler do
|
||||||
|
|
||||||
Logger.info("Building response body.")
|
Logger.info("Building response body.")
|
||||||
|
|
||||||
|
|
||||||
a = String.split(request, "\n\r")
|
a = String.split(request, "\n\r")
|
||||||
[get_line | _rest] = a
|
[get_line | _rest] = a
|
||||||
Logger.info "get-line #{get_line}"
|
Logger.info("get-line #{get_line}")
|
||||||
|
|
||||||
|
|
||||||
b = String.split(get_line, " ")
|
b = String.split(get_line, " ")
|
||||||
[request_type | rest] = b
|
[request_type | rest] = b
|
||||||
Logger.info "REQ_TYPE #{request_type}"
|
Logger.info("REQ_TYPE #{request_type}")
|
||||||
[path | _rest] = rest
|
[path | _rest] = rest
|
||||||
Logger.info("PATH #{path}")
|
Logger.info("PATH #{path}")
|
||||||
|
|
||||||
|
@ -44,23 +42,23 @@ defmodule RlRepo.ClientHandler do
|
||||||
split_path_length = length(split_path)
|
split_path_length = length(split_path)
|
||||||
|
|
||||||
# TODO: Handle routing here.
|
# TODO: Handle routing here.
|
||||||
case split_path_length do
|
case split_path_length do
|
||||||
1 ->
|
1 ->
|
||||||
RlRepo.Router.parse_1_segment_path(split_path)
|
RlRepo.Router.parse_1_segment_path(split_path)
|
||||||
|> RlRepo.Router.route_1(request_type)
|
|> RlRepo.Router.route_1(request_type)
|
||||||
|
|
||||||
3 ->
|
3 ->
|
||||||
RlRepo.Router.parse_3_segment_path(split_path)
|
RlRepo.Router.parse_3_segment_path(split_path)
|
||||||
|> RlRepo.Router.route_3(request_type)
|
|> RlRepo.Router.route_3(request_type)
|
||||||
|
|
||||||
4 ->
|
4 ->
|
||||||
RlRepo.Router.parse_4_segment_path(split_path)
|
RlRepo.Router.parse_4_segment_path(split_path)
|
||||||
|> RlRepo.Router.route_4(request_type)
|
|> RlRepo.Router.route_4(request_type)
|
||||||
|
|
||||||
# Note: Error handling.
|
# Note: Error handling.
|
||||||
_ ->
|
_ ->
|
||||||
RlRepo.Router.status_404()
|
RlRepo.Router.status_404()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_return_code_string(return_code) do
|
def create_return_code_string(return_code) do
|
||||||
|
@ -73,13 +71,14 @@ defmodule RlRepo.ClientHandler do
|
||||||
def create_response_header(body) do
|
def create_response_header(body) do
|
||||||
{return_code, content_type, body} = body
|
{return_code, content_type, body} = body
|
||||||
html_ver = "1.1"
|
html_ver = "1.1"
|
||||||
"""
|
|
||||||
HTTP/#{html_ver} #{create_return_code_string(return_code)}\r
|
"""
|
||||||
Content-Type: #{content_type}\r
|
HTTP/#{html_ver} #{create_return_code_string(return_code)}\r
|
||||||
Content-Length: #{byte_size(body)}\r
|
Content-Type: #{content_type}\r
|
||||||
\r
|
Content-Length: #{byte_size(body)}\r
|
||||||
#{body}
|
\r
|
||||||
"""
|
#{body}
|
||||||
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_response(response, client_socket) do
|
def write_response(response, client_socket) do
|
||||||
|
|
11
lib/json.ex
11
lib/json.ex
|
@ -3,9 +3,14 @@ defmodule RlRepo.Json do
|
||||||
|
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
"""
|
"""
|
||||||
def fmt_as_json(a) do
|
def map_to_json(a) do
|
||||||
"{\"pkg_name\": \"abc\"," <>
|
# msg = ""
|
||||||
"\"" <>
|
"{" <>
|
||||||
|
kv_step("", "key", "value") <>
|
||||||
"}"
|
"}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def kv_step(msg, key, value) do
|
||||||
|
msg <> "\"#{key}\" : \"#{value}\""
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
58
lib/repo/repo_lookup.ex
Normal file
58
lib/repo/repo_lookup.ex
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
defmodule RepoLookup do
|
||||||
|
require Logger
|
||||||
|
|
||||||
|
def is_repo?(string) do
|
||||||
|
send(:repo_lookup, {"is_repo?", string, self()})
|
||||||
|
|
||||||
|
receive do
|
||||||
|
{"is_repo", value} ->
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def repo_contains_pkg?(repo_name, pkg) do
|
||||||
|
send(:repo_lookup, {"repo_contains_pkg?", repo_name, pkg, self()})
|
||||||
|
|
||||||
|
receive do
|
||||||
|
{"repo_contains_pkg?", value} ->
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def alias_check() do
|
||||||
|
# Set this process up to allow finding it via the :repo_lookup alias
|
||||||
|
|
||||||
|
if Enum.member?(Process.registered(), :repo_lookup) do
|
||||||
|
# Process.unalias(:repo_lookup)
|
||||||
|
end
|
||||||
|
|
||||||
|
Process.register(self(), :repo_lookup)
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_repo_lookup() do
|
||||||
|
Logger.info("Repo spinning.")
|
||||||
|
alias_check()
|
||||||
|
|
||||||
|
Logger.info("Logger process alias registered")
|
||||||
|
loop_repo_lookup()
|
||||||
|
end
|
||||||
|
|
||||||
|
def loop_repo_lookup() do
|
||||||
|
Logger.info("Repo Lookup loop")
|
||||||
|
|
||||||
|
receive do
|
||||||
|
{"is_repo", value, return_pid} ->
|
||||||
|
Logger.info("Checking #{value}")
|
||||||
|
send(return_pid, {"is_repo", false})
|
||||||
|
|
||||||
|
{"repo_contains_pkg?", repo_name, pkg_name, pid}
|
||||||
|
->
|
||||||
|
Logger.info "Checking repo #{repo_name} for pkg #{pkg_name}"
|
||||||
|
send(pid, {"repo_contains_pkg?", false})
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
loop_repo_lookup()
|
||||||
|
end
|
||||||
|
end
|
|
@ -13,6 +13,12 @@ defmodule RlRepo do
|
||||||
|
|
||||||
Logger.info("Listening on port #{port}")
|
Logger.info("Listening on port #{port}")
|
||||||
|
|
||||||
|
# Spawn RepoLookup
|
||||||
|
|
||||||
|
Task.Supervisor.start_child(RlRepo.QueryServices, fn ->
|
||||||
|
RepoLookup.start_repo_lookup()
|
||||||
|
end)
|
||||||
|
|
||||||
loop_acceptor(listener_socket)
|
loop_acceptor(listener_socket)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,8 @@ defmodule RlRepo.Application do
|
||||||
port = String.to_integer(System.get_env("PORT") || "10002")
|
port = String.to_integer(System.get_env("PORT") || "10002")
|
||||||
|
|
||||||
children = [
|
children = [
|
||||||
|
{Task.Supervisor, name: RlRepo.QueryServices},
|
||||||
|
|
||||||
# Starts a worker by calling: RlRepo.Worker.start_link(arg)
|
# Starts a worker by calling: RlRepo.Worker.start_link(arg)
|
||||||
# {RlRepo.Worker, arg}
|
# {RlRepo.Worker, arg}
|
||||||
Supervisor.child_spec({Task, fn -> RlRepo.start(port) end}, restart: :permanent)
|
Supervisor.child_spec({Task, fn -> RlRepo.start(port) end}, restart: :permanent)
|
||||||
|
|
|
@ -28,17 +28,26 @@ defmodule RlRepo.Router do
|
||||||
Logger.info("#{request_type}")
|
Logger.info("#{request_type}")
|
||||||
{repo_name, pkg_name, action} = path
|
{repo_name, pkg_name, action} = path
|
||||||
|
|
||||||
if action == "info" do
|
if RepoLookup.is_repo?(repo_name) do
|
||||||
Logger.info("fetching json")
|
Logger.info("#{repo_name} is a repo.")
|
||||||
|
else
|
||||||
|
Logger.info("#{repo_name} is not a repo.")
|
||||||
end
|
end
|
||||||
|
|
||||||
{200, "application/json", "{\"pkg_name\": \"#{pkg_name}\"}"}
|
if action == "info" do
|
||||||
|
Logger.info("fetching json")
|
||||||
|
{200, "application/json", "{\"pkg_name\": \"#{pkg_name}\"}"}
|
||||||
|
end
|
||||||
|
|
||||||
|
placeholders = %{}
|
||||||
|
# {200, "text/html", "<p>REPO_PAGE</p>"}
|
||||||
|
{200, "text/html", RlRepo.Template.template("repo", placeholders)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def route_4(path, request_type) do
|
def route_4(path, request_type) do
|
||||||
Logger.info("#{request_type}")
|
Logger.info("#{request_type}")
|
||||||
# {repo_name, sub_repo_name, pkg_name, action} = path
|
# {repo_name, sub_repo_name, pkg_name, action} = path
|
||||||
{200, "application/json", RlRepo.Json.fmt_as_json()}
|
{200, "application/json", RlRepo.Json.map_to_json(%{})}
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_1_segment_path(path) do
|
def parse_1_segment_path(path) do
|
||||||
|
|
Loading…
Reference in a new issue