Skip to content

Commit

Permalink
structs
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 23, 2022
1 parent b7e95df commit a2b8f9b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 65 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

3. pragstudio-elixir-03-transforms - [Transforms](https://github.com/skedaddl3/elixir-pragstudio/blob/0e49501f2c43fd64deb6f886efa5d2b9adcf45ec/lib/handler.ex)

4. pragstudio-elixir-04-parse - [Parse]()
4. pragstudio-elixir-04-parse - [Parse](https://github.com/skedaddl3/elixir-pragstudio/blob/fcfada345ca5dfc15c08f9f0c8c4b9db2ffa3cd7/lib/handler.ex)

5. pragstudio-elixir-05-route-response - [Route Response]()
5. pragstudio-elixir-05-route-response - [Route Response](https://github.com/skedaddl3/elixir-pragstudio/blob/cb18904da76e0371fd52780d73d07b4d99d25596/lib/handler.ex)

6. pragstudio-elixir-06-function-clauses - [Function Clauses]()
6. pragstudio-elixir-06-function-clauses - [Function Clauses](https://github.com/skedaddl3/elixir-pragstudio/blob/87aa39c2f0471c937572f04be0211d1ed6c77880/lib/handler.ex)

7. pragstudio-elixir-07-params-status - [Params Status]()
7. pragstudio-elixir-07-params-status - [Params Status](https://github.com/skedaddl3/elixir-pragstudio/blob/9167a1ef53cc5da16c0b3acc56797884f0b7dad8/lib/handler.ex)

8. pragstudio-elixir-08-rewriting - [Rewriting]()
8. pragstudio-elixir-08-rewriting - [Rewriting](https://github.com/skedaddl3/elixir-pragstudio/blob/39956fdd336f8d9b5cd57aba9a5643732bbd4d06/lib/handler.ex)

9. pragstudio-elixir-09-serve-files - [Serve Files]()
9. pragstudio-elixir-09-serve-files - [Serve Files](https://github.com/skedaddl3/elixir-pragstudio/commit/0f7f211798d4a50d4dfb10ce2abc2d8b7215e4c1)

10. pragstudio-elixir-10-module-attributes - [Module Attributes]()
10. pragstudio-elixir-10-module-attributes - [Module Attributes](https://github.com/skedaddl3/elixir-pragstudio/commit/cb2934a118192f48d737073de9ef1693aa70e935#diff-51177fba3219e9037adef9c75b1e0d2ea935315d05287a61d8892f1b3764edd4)

11. pragstudio-elixir-11-organizing-code - [Organizing Code]()
11. pragstudio-elixir-11-organizing-code - [Organizing Code](https://github.com/skedaddl3/elixir-pragstudio/commit/b7e95dfc6d780c3b91ecfa01d721c59c24368830)

12. pragstudio-elixir-12-structs - [Structs]()

Expand Down
18 changes: 18 additions & 0 deletions lib/conv.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Servy.Conv do
defstruct method: "", path: "", resp_body: "", status: nil

def full_status(conv) do
"#{conv.status} #{status_reason(conv.status)}"
end

defp status_reason(code) do
%{
200 => "OK",
201 => "Created",
401 => "Unauthorized",
403 => "Forbidden",
404 => "Not Found",
500 => "Internal Server Error"
}[code]
end
end
94 changes: 42 additions & 52 deletions lib/handler.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule Servy.Handler do
alias Servy.Conv
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__)
# @pages_path Path.expand("../pages", __DIR__)
@doc "Transforms the request into a response."
def handle(request) do
request
Expand All @@ -18,82 +19,71 @@ defmodule Servy.Handler do
# route(conv, conv.method, conv.path)
# end

def route(%{method: "GET", path: "/wildthings"} = conv) do
def route(%Conv{method: "GET", path: "/wildthings"} = conv) do
%{conv | status: 200, resp_body: "Bears, Lions, Tigers"}
end

def route(%{method: "GET", path: "/bears"} = conv) do
def route(%Conv{method: "GET", path: "/bears"} = conv) do
%{conv | status: 200, resp_body: "Teddy, Smokey, Paddington"}
end

def route(%{method: "GET", path: "/bears" <> id} = conv) do
def route(%Conv{method: "GET", path: "/bears" <> id} = conv) do
%{conv | status: 200, resp_body: "Bear #{id}"}
end

def route(%{method: "GET", path: "/about"} = conv) do
Path.expand("../pages", __DIR__)
|> Path.join("about.html")
|> File.read!()
|> handle_file(conv)
end

def handle_file({:ok, content}, conv) do
%{conv | status: 200, resp_body: content}
end
# def route(%Conv{method: "GET", path: "/about"} = conv) do
# Path.expand("../pages", __DIR__)
# |> Path.join("about.html")
# |> File.read!()
# |> handle_file(conv)
# end

def handle_file({:error, :enoent}, conv) do
%{conv | status: 404, resp_body: "File Not Found!"}
end
# def handle_file({:ok, content}, conv) do
# %{conv | status: 200, resp_body: content}
# end

def handle_file({:error, reason}, conv) do
%{conv | status: 500, resp_body: "File error: #{reason}"}
end
# def handle_file({:error, :enoent}, conv) do
# %{conv | status: 404, resp_body: "File Not Found!"}
# end

# def route(%{method: "GET", path: "/about"} = conv) do
# file =
# Path.expand(
# "../pages",
# __DIR__
# )
# |> Path.join("about.html")

# case File.read(file) do
# {:ok, content} ->
# %{conv | status: 200, resp_body: content}

# {:error, :enoent} ->
# %{conv | status: 404, resp_body: "File Not Found!"}

# {:error, reason} ->
# %{conv | status: 500, resp_body: "File error: #{reason}"}
# %{conv | status: 200, resp_body: "contents of file"}
# end
# def handle_file({:error, reason}, conv) do
# %{conv | status: 500, resp_body: "File error: #{reason}"}
# end

def route(%{method: "GET", path: "/about"} = conv) do
file =
Path.expand(
"../pages",
__DIR__
)
|> Path.join("about.html")

case File.read(file) do
{:ok, content} ->
%{conv | status: 200, resp_body: content}

{:error, :enoent} ->
%{conv | status: 404, resp_body: "File Not Found!"}

{:error, reason} ->
%{conv | status: 500, resp_body: "File error: #{reason}"}
%{conv | status: 200, resp_body: "contents of file"}
end
end

def route(%{path: path} = conv) do
%{conv | status: 404, resp_body: "No #{path} here!"}
end

def format_response(conv) do
def format_response(%Conv{} = conv) do
"""
HTTP/1.1 #{conv.status} #{status_reason(conv.status)}
HTTP/1.1 #{Conv.full_status(conv)}
Content-Type: text/html
Content-Length: #{String.length(conv.resp_body)}
#{conv.resp_body}
"""
end

defp status_reason(code) do
%{
200 => "OK",
201 => "Created",
401 => "Unauthorized",
403 => "Forbidden",
404 => "Not Found",
500 => "Internal Server Error"
}[code]
end
end

request = """
Expand Down
4 changes: 3 additions & 1 deletion lib/parser.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
defmodule Servy.Parser do
alias Servy.Conv

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

%{method: method, path: path, resp_body: "", status: nil}
%Conv{method: method, path: path}
end
end
9 changes: 5 additions & 4 deletions lib/plugins.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
defmodule Servy.Plugins do
alias Servy.Conv
@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 track(%Conv{} = conv), do: conv

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

def rewrite_path(conv), do: conv
def rewrite_path(%Conv{} = conv), do: conv

def log(conv), do: IO.inspect(conv)
def log(%Conv{} = conv), do: IO.inspect(conv)
end

0 comments on commit a2b8f9b

Please sign in to comment.