Created
January 7, 2021 04:07
-
-
Save sb8244/6ff6af1bf01febddddb76f0cb00b4b8a 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 TestRouter do | |
def build(routes) do | |
contents = | |
quote do | |
use Plug.Router | |
plug :match | |
plug :dispatch | |
Enum.map(unquote(Macro.escape(routes)), fn %{match: match, assign: assign} -> | |
get match, [assigns: assign] do | |
var!(conn) | |
end | |
end) | |
end | |
Module.create(__MODULE__.Handler, contents, Macro.Env.location(__ENV__)) | |
end | |
end | |
router = TestRouter.build([ | |
%{match: "/hello", assign: %{a: 1, b: 2}}, | |
%{match: "/other", assign: %{c: 3, d: "a"}}, | |
%{match: "/widgets/:id/something/:nested", assign: %{}}, | |
]) | |
conn = TestRouter.Handler.call(Plug.Test.conn(:get, "/widgets/1/something/else", ""), TestRouter.Handler.init([])) | |
# Will use conn then to provide `params` and certain assigned variables (stored in DB) into the Front Controller |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment