kif

kif

Creating routes from a file

I’m trying to migrate a website to Phoenix. I have a JSON file with routes definitions.

How can I route traffic base on my file definitions?

I fond this post but it only adds more questions.

Most Liked

rogerweb

rogerweb

I’m pretty sure if you create your own plug it would be more elegant, but you can read your json file from within your endpoint.ex, iterate over the routes and add a Plug.Static for each.

I did a simple POC just to show you what can be done:

  for route <- ["aaa", "bbb", "ccc"] do
    plug Plug.Static,
      at: "/" <> route,   # the path as in the browser
      from: "/home/roger/code/temp/hello/" <> route,  # the path in the filesystem
      gzip: false
  end

Provided you have an index.html file in /home/roger/code/temp/hello/aaa/, you can access it from http://localhost:4000/aaa/index.html. The path in the filesystem doesn’t need to be directly related to the path in the URL, it was just an example. I would take a look at the documentation of Plug.Static to check all the options.

rogerweb

rogerweb

Considering you have content, meta-data, route and status, it looks we are talking about a CMS here, isn’t it?

If that’s not the case, if your your site is static then you may read the file from a one-time script (elixir or not) that simply organizes the content files in a directory structure that can then be served statically, by Plug.Static or even an external webserver like nginx.

If you really want to create Phoenix routes dynamically, you could read the JSON content from your config.exs file and have is as a compilation-time list that you can iterate over from your routes.ex file to dynamically create the routes. Of course if you change the JSON then you would have to re-compile your application.

Last, you could have a dynamic website with a single route with a parameter in the URL path that you use to match against a keyword (for instance, “vertex-cache”). With the keyword, you would fetch data from a database or from an in-memory structure with the contents of your JSON to serve the correct content.

kif

kif

I didn’t know that you can add plugs in a for loop like this! Also following your suggestion I’m trying to see if I can make my custom plug that I can modify static path dynamically. Still struggling but thanks for this direction! Need some time to process this information.

Where Next?

Popular in Questions Top

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
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement