Skip to content

Instantly share code, notes, and snippets.

@luizpvas
Created January 11, 2016 12:58
Show Gist options
  • Save luizpvas/d12b986a42eac6267e7a to your computer and use it in GitHub Desktop.
Save luizpvas/d12b986a42eac6267e7a to your computer and use it in GitHub Desktop.
defmodule MyRouter do
use Plug.Router
# Starts the server
def start do
Plug.Adapters.Cowboy.http MyRouter, []
end
plug :my_handle # custom plug
plug :match
plug :dispatch
def my_handle(conn, _opts) do
Plug.Conn.register_before_send(conn, fn conn ->
# Doesn't show up in console.
IO.puts "== cleaning up =="
conn
end)
# This is printed to the console.
IO.puts "== setting up =="
conn
end
get "/" do
send_resp(conn, 200, "world")
end
match _ do
send_resp(conn, 404, "oops")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment