I’m building an app that would handle request for bare domain as well as for subdomains. Each subdomain is client space.
I have very different requirements for bare domain routing and for subdomain routing (same paths with very different functions). So I want to split router in two, one for root and one for subdomains (all subdomains have the same routing).
After researching I arrived to using forward function in the main router to forward to my plug:
defmodule AppWeb.Plugs.RouterSelector do
@behaviour Plug
@domain_init AppWeb.RouterDomain.init([])
@root_init AppWeb.RouterRoot.init([])
@impl true
def init(opts), do: opts
@impl true
def call(%Plug.Conn{} = conn, _opts) do
if conn.assigns.has_domain do
AppWeb.RouterDomain.call(conn, @domain_init)
else
AppWeb.RouterRoot.call(conn, @root_init)
end
|> Plug.Conn.halt()
end
end
Each of the routers called in this plug are “regular ones”.
I’m not sure if that is the right way to do it. And another thing that puzzles me it that I needed to add this |> Plug.Conn.halt() at the end of if block. Without it it told me about double rendering (I expected that the normal router called here will halt the request once done).
Is it the correct way to do it? And why (if) is there the explicit halt?
Thanks!






















