Dylan-aidkr

Dylan-aidkr

trying to split the large router file

this is some simple representation of my router

# part of MyAppWeb.Router.ex
  scope "/shop/:shop_id" do
    scope "/users" do
      live "/"
      live "/:user_id"
      # large sub routes
      ...
    end

    scope "/items" do
      live "/"
      live "/:item_id"
      # large sub routes
      ...
    end
  end

I searched, and it seems there are 3 options for splitting router.

1. using “forward”

not possible, as it does not allow to forward to route with dynamic segment

== Compilation error in file lib/my_app_web/router.ex ==
** (ArgumentError) dynamic segment "/shop/:shop_id/users" not allowed when forwarding. Use a static path instead

2. using “match”

not able to catch error with verified routes

scope "/shop/:shop_id" do
  match :*, "/users/*path", MyAppWeb.UserRouter, :any
  match :*, "/items/*path", MyAppWeb.ItemRouter, :any
end

with this method invalid path like a “/shop/1234/users/1234/no-exist-path” was not caught by the verified routes.

3. plug routers to Endpoint.ex

not sure if it would work. how to declare verified routes on this setting?

 # part of Endpoint.ex
defmodule MyAppWeb.Endpoint do
...
plug MyAppWeb.Router
plug MyAppWeb.UserRouter
plug MyAppWeb.ItemRouter
end

code above is not working. it seems the Router return NoRouteError before UserRouter.
there is a solution for this(Using multiple routers in Phoenix - #3 by alexgleason)
but still not sure how deal with the VerifiedRoutes.
i need to fix code below to properly handle multiple router.

# part of MyAppWeb.ex
use Phoenix.VerifiedRoutes,
      endpoint: MyAppWeb.Endpoint,
      router: [MyAppWeb.Router, MyAppWeb.UserRouter], # array of router is not valid input
      statics: MyAppWeb.static_paths()

please help!

Showing Posts 1 to 8

stephane

stephane

Hey @Dylan-aidkr,

One simple solution is to create a macro to split your router.

example

  require MySubRoutes
  ....
# part of MyAppWeb.Router.ex
  scope "/shop/:shop_id" do
    scope "/users" do
      live "/"
      live "/:user_id"
      # large sub routes
      ...
    end

    MySubRoutes.items()
  end
defmodule MySubRoutes do
  defmacro items do
    quote do
       scope "/items" do
         live "/"
         live "/:item_id"
       
         ...
       end
    end
  end
end

This allow you to keep your router clean and forward easily path params.

Dylan-aidkr

Dylan-aidkr OP

sorry i forgot to mention that macro is not an option.

actually i did that in my first attempt, and got a request from coworker to solve it without the macro.

maybe it’s because a macro makes it hard to detect errors,
as it only shows the error while calling macro, not the wrong code in macro itself

anyway, thanks for the reply.

stephane

stephane

You’re right about macro makes it harder to debug / detect errors.

But in this case, your custom macro is just to use Phoenix.(Live).Router macros. Actually the phoenix router contains essentially macros (live, scope, resources…). There are no custom application logic inside yours.

This solution makes it easy to split into smaller files, handle params and upper scopes and verified routes still works :slight_smile:
But very interested if any other ways to solve all of these issues at once.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

What approach did they suggest as an alternative?

mentero

mentero

We went with

match :*, "/users/*path", MyAppWeb.UserRouter

for router splitting (using macros was causing frequent big recompiles for us)
We are used to alias the Router we want to refer before using path helpers

  alias MyAppWeb.UserRouter.Helpers, as: Routes
  #...
  Routes.users_path(...)

We did not move to verified routes yet, but I would assume we would need to move from aliasing to use Phoenix.VerifiedRoutes

chrismccord

chrismccord

Creator of Phoenix

You can either use Phoenix.VerifiedRoutes, router: ... in the context where you want to use different routers, or use Phoenix.VerifiedRoutes.path/3 and explicitly pass the router:

  @doc ~S'''
  Generates the router path with route verification.

  See `sigil_p/1` for more information.

  Warns when the provided path does not match against the router specified
  in `use Phoenix.VerifiedRoutes` or the `@router` module attribute.

  ## Examples

      import Phoenix.VerifiedRoutes

      redirect(to: path(conn, MyAppWeb.Router, ~p"/users/top"))

      redirect(to: path(socket, MyAppWeb.Router, ~p"/users/#{@user}"))

      ~H"""
      <.link to={path(@uri, MyAppWeb.Router, ~p"/users?page=#{@page}")}>profile</.link>
      <.link to={path(@uri, MyAppWeb.Router, ~p"/users?#{@params}")}>profile</.link>
      """
  '''
Dylan-aidkr

Dylan-aidkr OP

they first suggested to use ‘forward’ or ‘match’, as most blog posts recommended.

so i tried them and found some issues i posted.

as those blog posts were from older versions. it was hard to find any post mentioning verified routes.

Dylan-aidkr

Dylan-aidkr OP

Thank you all for the replies. :partying_face:

I shared the search results with my team, and concluded to just use macro for now.

some of existing codes using the old route helper dynamically,
which made a refactoring a bit confusing with the separated router modules.

The macro approach seems to be sufficient.
It has minimal impact on other codes,
and my original intention was to improve readability of the large router.

Once again, thank you all!

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews