It’s weird because this is perfectly valid:
defmodule Servy.Handler do
alias Servy.Conv
@moduledoc "Handles HTTP requests"
@pages_path Path.expand("pages", File.cwd!())
@doc "Transforms the request into a response."
def handle(request) do
request
|> route
|> format_response
end
def route(%Conv{method: "GET", path: "/wildthings"} = conv) do
%{conv | status: 200, resp_body: "Bears, Lions, Tigers"}
end
def route(%Conv{method: "GET", path: "/pages/" <> file} = conv) do
Path.expand("../../pages", __DIR__)
|> Path.join(file <> ".html")
|> File.read()
|> handle_file(conv)
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 route(%Conv{path: path} = conv) do
%{conv | status: 404, resp_body: "No #{path} here"}
end
def format_response(%Conv{} = conv) do
# TODO: Use values in the map to create an HTTP response string:
"""
HTTP/1.1 #{Conv.full_status(%Conv{} = conv)}
Content-Type: text/html
Content-Length: #{byte_size(conv.resp_body)}
#{conv.resp_body}
"""
end
end
request = """
GET /wildthings HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*
"""
response = Servy.Handler.handle(request)
IO.puts(response)


















