hauleth

hauleth

Define root path per router instead on endpoint level only

I am building application where I have separate routers for each part of the API (REST, UI, and GraphQL):

defmodule MyApp.Router do
  use Phoenix.Router

  forward("/graphql", MyApp.GraphQL.Router)
  forward("/api", MyApp.REST.Router)
  forward("/", MyApp.UI.Router)
end

Currently there is no problem with that as I can specify “sub-path” in API documentation (OpenAPI), but I see potential problem in future where the amount of routers can grow. Namely imagine situation where I would need to add new router, ex. MyApp.Foo.Router within scope /foo but within that controller I would need it to generate paths with that prefix, so I will need to workaround that by either remember to provide handcrafted %URL{} as the first parameter to the helper functions or to write it as /foo/<%= helper_path(…) %> (that second option will not work for other helpers though). What I would love to see is option :path similar to the Phoenix.Endpoint one passed either to Phoenix.Router.__using__/1 or (even better) to generated Router.init/1. That second option is even better as it allows me to define used route in the same place I define forwarding.

What are your opinions on such feature? Or maybe I missed something that is already possible.

First Post!

nivanson

nivanson

I don’t know how to get rid of the routing issue for your solution. For this specific problem I think having separate endpoints with a master proxy is an acceptable solution. One can define an endpoint that proxies the others based on whatever logic you want. Domain. Route. Whatever. See this example.

https://github.com/wojtekmach/acme_bank/blob/master/apps/master_proxy/lib/master_proxy/plug.ex

Where Next?

Popular in Questions Top

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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement