Start building out model; query all journals and pages from filesystem

This commit is contained in:
Ryan Pandya
2022-08-30 11:24:56 -07:00
parent cfd1738ef3
commit 51a7b8be93
12 changed files with 148 additions and 11 deletions
+17 -11
View File
@@ -3,16 +3,22 @@ defmodule Logsrv do
Documentation for `Logsrv`.
"""
@doc """
Hello world.
## Examples
iex> Logsrv.hello()
:world
"""
def hello do
:world
def dir do
Application.get_env(:logsrv, :dir)
end
def dir(subdir) do
"#{dir()}/#{subdir}"
end
def journals do
dir(:journals)
|> File.ls!
end
def pages do
dir(:pages)
|> File.ls!
end
end
+9
View File
@@ -10,6 +10,15 @@ defmodule Logsrv.Application do
children = [
# Starts a worker by calling: Logsrv.Worker.start_link(arg)
# {Logsrv.Worker, arg}
{
Plug.Cowboy,
scheme: :http,
plug: Logsrv.Router,
options:
[
port: Application.get_env(:logsrv, :port)
]
}
]
# See https://hexdocs.pm/elixir/Supervisor.html
+52
View File
@@ -0,0 +1,52 @@
defmodule Logsrv.Router do
# Bring Plug.Router module into scope
use Plug.Router
# Attach the Logger to log incoming requests
plug(Plug.Logger)
# Tell Plug to match the incoming request with the defined endpoints
plug(:match)
# Once there is a match, parse the response body if the content-type
# is application/json. The order is important here, as we only want to
# parse the body if there is a matching route.(Using the Jayson parser)
plug(Plug.Parsers,
parsers: [:json],
pass: ["application/json"],
json_decoder: Jason
)
# Dispatch the connection to the matched handler
plug(:dispatch)
# Handler for GET request with "/" path
get "/" do
send_resp(conn, 200, "OK")
end
get "/journals" do
journals =
Logsrv.journals
|> Jason.encode!() # Encode the list to a JSON string
conn
|> put_resp_content_type("application/json")
|> send_resp(200, journals) # Send a 200 OK response with the posts in the body
end
get "/pages" do
pages =
Logsrv.pages
|> Jason.encode!() # Encode the list to a JSON string
conn
|> put_resp_content_type("application/json")
|> send_resp(200, pages) # Send a 200 OK response with the posts in the body
end
# Fallback handler when there was no match
match _ do
send_resp(conn, 404, "Not Found")
end
end