Skip to content

Instantly share code, notes, and snippets.

@lpil
Last active April 29, 2018 10:57
Show Gist options
  • Save lpil/a5a0f62a26b17157cd2f2917b7bcbec3 to your computer and use it in GitHub Desktop.
Save lpil/a5a0f62a26b17157cd2f2917b7bcbec3 to your computer and use it in GitHub Desktop.
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
defmodule Router do
def handle(:GET, ["ping"], req) do
{200, [], "pong"}
end
def handle(_method, _path, _req) do
{404, [], "Not found"}
end
end
@CrowdHailer
Copy link

What is are _args second argument in handle function?

@lpil
Copy link
Author

lpil commented Feb 13, 2018

@crowdhailed Anything you want, you pass it in the child spec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment