-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7e95df
commit a2b8f9b
Showing
5 changed files
with
76 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |