Skip to content

Instantly share code, notes, and snippets.

@henrik
Created June 10, 2015 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/63b31abee1e3f9ee714f to your computer and use it in GitHub Desktop.
Save henrik/63b31abee1e3f9ee714f to your computer and use it in GitHub Desktop.
Wanted to understand what Plug.Router does to get a "conn" available, so reimplemented it (without peeking).
defmodule Toy.GetContext do
def send_resp(conn, code, text) do
IO.puts "Response! conn: #{conn}, code: #{code}, text: #{text}"
end
def conn do
"the conn"
end
end
defmodule Toy.PlugRouter do
defmacro __using__(_opts) do
quote do
import Toy.PlugRouter
end
end
defmacro get(_path, do: block) do
quote do
import Toy.GetContext
unquote(block)
end
end
end
defmodule Toy.Router do
use Toy.PlugRouter
get "/hello" do
send_resp(conn, 200, "Hi!")
end
end
@henrik
Copy link
Author

henrik commented Jun 10, 2015

@rbishop
Copy link

rbishop commented Jul 19, 2015

@henrik Close! Plug makes use of the var! macro for injecting a variable into your route context: https://github.com/elixir-lang/plug/blob/master/lib/plug/router.ex#L360

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