TEMPLATE: library

This commit is contained in:
Able 2025-02-18 10:49:37 -06:00
parent 07d553ab92
commit 4de928e01e
6 changed files with 77 additions and 6 deletions

View file

@ -1,5 +1,16 @@
# RlRepo
## Usage
To run.
```sh
elixir --sname foo -S mix run --no-halt
```
To run an observer.
```sh
erl -setcookie MyCookie -run observer
```
## API Usage
/<repo_name>/<?sub_repo_name>/<pkg_name>/download

View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<center>
<h1>404 Not Found</h1>
<p>Reason: ~(reason) </p>
</center>
</body>
</html>

View file

@ -31,7 +31,7 @@ defmodule RlRepo.ClientHandler do
# TODO: Handle routing here.
a = String.split(request, "\n\r");
Logger.info a
# Logger.info a
[get_line | rest] = a
b = String.split(get_line, " ");
@ -40,22 +40,24 @@ defmodule RlRepo.ClientHandler do
Logger.info "#{request_type} #{path}"
# Parse path
split_path = String.split(path, "/");
split_path = String.split(path, "/", trim: true);
# Ignore the first slash.
[_ | rest ] = split_path
# [_ | rest ] = split_path
split_path_length = length(rest)
Logger.info split_path_length
split_path_length = length(split_path)
ret = case split_path_length do
1 -> RlRepo.Router.parse_1_segment_path(rest)
|> RlRepo.Router.route_1(request_type)
3 ->
RlRepo.Router.parse_3_segment_path(rest)
|> RlRepo.Router.route_3(request_type)
4 ->
RlRepo.Router.parse_4_segment_path(rest)
|> RlRepo.Router.route_4(request_type)
# Note: Error handling.
_ -> RlRepo.Router.status_404()
end
end

View file

@ -3,6 +3,17 @@ defmodule RlRepo.Router do
@moduledoc """
"""
def status_404() do
placeholders = %{ "reason" => "Unknown"}
{404, "text/html", RlRepo.Template.template("404-error", placeholders)}
end
def route_1(path, request_type) do
{200, "text/html", "Hi"}
end
def route_3( path, request_type) do
Logger.info "#{request_type}"
{repo_name, pkg_name, action} = path
@ -20,6 +31,10 @@ defmodule RlRepo.Router do
{200, "text/html", "<p>Hi</p>"}
end
def parse_1_segment_path(path) do
[repo_name | _ ] = path
{repo_name}
end
def parse_3_segment_path(path) do
[repo_name | rest ] = path

29
lib/templating.ex Normal file
View file

@ -0,0 +1,29 @@
defmodule RlRepo.Template do
require Logger
def template(template_name, replacements) do
Logger.info "Loading template #{template_name}"
{:ok, body} = File.read("assets/html/#{template_name}.html")
# TODO: Find all template start '~(' and all template ends ')'
[before_placeholder | rest] = split_path = String.split(body, "~(");
[abc | _] = rest
[placeholder_name | after_placeholder] = String.split(abc, ")");
# Logger.info rest
{:ok, replace} = Map.fetch(replacements, placeholder_name)
# next = String.split(rest, ")");
Logger.info "Replacing #{placeholder_name} with #{replace}"
[after_placeholder| _] = after_placeholder
a = before_placeholder <> replace <> after_placeholder
# TODO: use the map to populate placeholders
a
# body
end
end

View file

@ -4,6 +4,7 @@ defmodule RlRepo.MixProject do
def project do
[
app: :rl_repo,
cookie: "MyCookie",
version: "0.1.0",
elixir: "~> 1.12",
start_permanent: Mix.env() == :prod,