MisterD

MisterD

Phoenix - New Route from User Input - Adding Route Programmatically

Hi,
I was thinking about simple user settings for Permalinks.
Something like Wordpress has: Customize permalinks – Documentation – WordPress.org

The question is - how to make same behaviour in Phoenix ?
In case of predefined route structures - everything is clear, you simple make predefined routes in advance.

But what about custom one ?

  • How to make a new route structure from user input ?
  • How to add a new route programmatically ?

To my understanding the routes are build during compilation time, but does it mean it is not possible to create during runtime ?

If so then, what would the solution ?

  • Load routes from DB during compilation time and then if new routes are added - restart everything ?

Any ideas or an advice is very much welcome !

Marked As Solved

idi527

idi527

:waving_hand:

There wouldn’t be any performance issues if done correctly. I have a plug for webhooks that are generated at runtime to which I forward from the phoenix router module.

It works roughly like this

# in the router.ex
scope "/" do
  # ...
  forward "/webhooks", MyAppWeb.Plugs.WebHooks
end

# in plugs/web_hooks.ex
def call(%Plug.Conn{path_info: path_info, params: params} = conn, _opts) do
  if handler = get_webhook_handler(path_info) do
    handler.handle!(params)    
  end
  
  send_resp(conn, :ok, [])
end

Also Liked

chrismccord

chrismccord

Creator of Phoenix

I think you misinterpreted. You may be thinking of responses I have had to folks asking for matching on regex patterns as “wildcards” as me denying those kinds of matches because that would be much slower since we can’t pattern match, but a wildcard match, ie get "/*" or get "/foobar/*" will be super fast and just as fast as any other route match. As other have said, you can get what you by a either a wildcard forward match in the router, followed by your own custom lookup and dispatch code, which calls the appropriate controller (or plug), ie controller.call(controller.init(some_action))

easco

easco

A Plug accepts a Plug.Conn and returns a different Plug.Conn resource.

Part of the Plug.Conn structure is the “path_info”. Your plug could look at the Path Info and decide what function to call, pass he connection to it, and halt the propagation of the connection (so the the router never sees it).

Or, it could return a new Plug.Conn, with different path_info, let it propagate to the router, and the router could use its regular mechanism on the new path_info.

easco

easco

Just a thought off the top of my head. The Phoenix Router is “just a plug”. It tends to be the Plug at the end of the pipeline, but it is still a Plug. So it seems you could develop a Plug that looks up a new route in a dynamic data source (an ETS table, or database).

Where Next?

Popular in Questions Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement