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 !

First 10 of 13 Posts Switch mode

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

MisterD

MisterD OP

Thank you for reply!
I was thinking this way too…
I already tried to create a Plug and added that to the Pipeline.
I thought that I can analyse requested url and match through the regexp.
Once matched forward it to required function (handler).

But the problem is the following:

  • it gives the error, something like undefined route and gives me a list of valid routes.
MisterD

MisterD OP

I suppose the question is - during Plug execution, how to connect to the point when Phoenix analyses existing routes and give it the new route taken from DB or User Input ?

LostKobrakai

LostKobrakai

You cannot use the Router of plug or phoenix for the routing in this case. You need to create a wildcard route, which simply matches any url that finds it’s way to your endpoint and use custom plug to implement the routing to controllers or whatever needs to receive the request if the url is valid. The routers of plug and phoenix are based on compiled pattern matching functions, so they cannot be dynamic (or even support regex).

MisterD

MisterD OP

Thank you for reply.
I understand that I cannot use that as I already tried without success.
I would prefer not to create a wildcard route due to performance issues.

The question is - how to achieve the desired behaviour without losing performance.

  • It is possible to restart Router independently of Phoenix - I mean recompile it live ?
  • Just that peace of functionality, not the whole Phoenix ?
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
LostKobrakai

LostKobrakai

Do you have performance issues? If not I’d not worry to much until you have. If you do then what have you tried?

MisterD

MisterD OP

No, I don’t currently have that.

I just remember there was a post by Chris McCord stating that using a wildcard route would lead to performance problems.

MisterD

MisterD OP

Thank you very much for your example, I really appreciate it !

Probably this is something I should use for now, and if there would be any issues, then look for something else.

One more time, thank you everyone for help !

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.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement