cpgo
Plug router compiling routes from file
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_quotedis 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_quotedis safe? Does it have any performance implications?
Thanks in advance.
First Post!
kif
Popular in Questions
Hello, how can I check the Phoenix version ?
Thanks !
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









