Skip to content

Commit

Permalink
organizing code
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 23, 2022
1 parent cb2934a commit b7e95df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
28 changes: 2 additions & 26 deletions lib/handler.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Servy.Handler do
import Servy.Plugins, only: [rewrite_path: 1, log: 1, track: 1]
import Servy.Parser, only: [parse: 1]
@moduledoc "Handles HTTP requests."
@pages_path Path.expand("../pages", __DIR__)
@doc "Transforms the request into a response."
Expand All @@ -12,32 +14,6 @@ defmodule Servy.Handler do
|> format_response
end

@doc "Logs 404 requests"
def track(%{status: 404, path: path} = conv) do
IO.puts("Warning: #{path} is on the loose!")
conv
end

def track(conv), do: conv

def rewrite_path(%{path: "/wildlife"} = conv) do
%{conv | path: "/wildthings"}
end

def rewrite_path(conv), do: conv

def parse(request) do
[method, path, _] =
request
|> String.split("\n")
|> List.first()
|> String.split(" ")

%{method: method, path: path, resp_body: "", status: nil}
end

def log(conv), do: IO.inspect(conv)

# def route(conv) do
# route(conv, conv.method, conv.path)
# end
Expand Down
11 changes: 11 additions & 0 deletions lib/parser.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Servy.Parser do
def parse(request) do
[method, path, _] =
request
|> String.split("\n")
|> List.first()
|> String.split(" ")

%{method: method, path: path, resp_body: "", status: nil}
end
end
17 changes: 17 additions & 0 deletions lib/plugins.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Servy.Plugins do
@doc "Logs 404 requests"
def track(%{status: 404, path: path} = conv) do
IO.puts("Warning: #{path} is on the loose!")
conv
end

def track(conv), do: conv

def rewrite_path(%{path: "/wildlife"} = conv) do
%{conv | path: "/wildthings"}
end

def rewrite_path(conv), do: conv

def log(conv), do: IO.inspect(conv)
end

0 comments on commit b7e95df

Please sign in to comment.