Created
June 10, 2015 18:44
-
-
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).
This file contains 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 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 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
Previously: Reimplementing Ecto.Migration