Using multiple routers in Phoenix

Thank you for the tip. I figured out I could create a wrapper plug to call multiple routers and handle the exception.

endpoint.ex:

plug TribesWeb.MultiRouterPlug, routers: [TribesWeb.Router, Pleroma.Web.Router]

And the plug code:

defmodule TribesWeb.MultiRouterPlug do
  import Plug.Conn

  @behaviour Plug

  def init(opts \\ []) do
    Enum.into(opts, %{})
  end

  def call(conn, %{routers: routers}) when is_list(routers) do
    Enum.reduce(routers, conn, fn router, conn ->
      try do
        router.call(conn, [])
      rescue
        Phoenix.Router.NoRouteError -> conn
      end
    end)
  end
end
3 Likes