I’m trying to make something to help me when implementing BFFs (backend for frontends).
My idea is to have some route definitions on a file and read that file to create plug routes.
So far I have the following on my base plug router.
[%{verb: :get, path: "dynamic/:name"}, %{verb: :post, path: "post"}] # Will be read from a file
|> Enum.each(fn route ->
Code.eval_quoted(
{route.verb, [context: Routes, import: Plug.Router],
[
route.path,
[
do:
{{:., [], [{:__aliases__, [alias: false], [:Handler]}, :call]}, [],
[{:var!, [context: Routes, import: Kernel], [{:conn, [], Routes}]}, []]}
]
]},
[],
__ENV__
)
end)
It seems to be working, I can use all the plug routing goodiness and handle my proxying logic on my Handler
plug.
But I have some issues.
- I have no idea if
Code.eval_quoted
is the right aproach, I’m very new to macros and metaprogramming in Elixir - The routes are defined in compile time, so if I want to update the routes my options are recompiling/redeploying or maybe hot upgrades (I have no idea if its possible)
-
eval_quoted
is safe? Does it have any performance implications?
Thanks in advance.