cpgo

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_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.

First Post!

kif

kif

This is great question! I would like to know the same.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
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
Brian
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
Fl4m3Ph03n1x
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
belgoros
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
svb
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 Top

New
bsollish-terakeet
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
dblack
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
sergio
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
senggen
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
jason.o
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

We're in Beta

About us Mission Statement