Last active
April 29, 2018 10:57
-
-
Save lpil/a5a0f62a26b17157cd2f2917b7bcbec3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule Web do | |
require Logger | |
@behaviour :elli_handler | |
def handle(r, _args) do | |
Router.handle(:elli_request.method(r), :elli_request.path(r), r) | |
rescue | |
exception -> | |
Logger.error(inspect(exception)) | |
{500, [], "Internal server error"} | |
end | |
def handle_event(:request_complete, data, _args), do: log_request(data) | |
def handle_event(_event, _data, _args), do: :ok | |
defp log_request(data) do | |
[req, code, _headers, _body, {timings, _sizes}] = data | |
start = :erlang.convert_time_unit(timings[:request_start], :native, :microsecond) | |
finish = :erlang.convert_time_unit(timings[:request_end], :native, :microsecond) | |
request_microseconds = finish - start | |
msg = [ | |
to_string(:elli_request.method(req)), | |
?\s, | |
:elli_request.raw_path(req), | |
?\s, | |
to_string(code), | |
?\s, | |
to_string(request_microseconds), | |
"μs" | |
] | |
Logger.info(msg, request_microseconds: request_microseconds) | |
end | |
end |
This file contains hidden or 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
defmodule Router do | |
def handle(:GET, ["ping"], req) do | |
{200, [], "pong"} | |
end | |
def handle(_method, _path, _req) do | |
{404, [], "Not found"} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@crowdhailed Anything you want, you pass it in the child spec