dacz

dacz

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!

Marked As Solved

BartOtten

BartOtten

You could have one with hosts: topdomain.com and one scope without hosts set which is a catchall (the hosts field is converted to _ in the route match pattern)

Make sure the toplevel goes first.

Ps. Hosts should be a list of binaries, mobile editing is…less than ideal.

Also Liked

BartOtten

BartOtten

Seems you should be able to define two pipelines with different plugs and two scopes using these different pipelines (and even different controllers).

acrolink

acrolink

Usually for things like this I use nginx in front. Actually, I always use nginx in front.

BartOtten

BartOtten

Aa for the halt:

Halt stops the pipeline from proceeding following plugs. It does not halt the request.

So we are in pipeline land. Which clears things up.

Even though a sub phoenix router will send a response to finish the user request (after it halts), the parent plug pipeline that called the phoenix router does not receive that halt instruction; only the response.

So without the explicit halt in the router selector plug, the plug pipeline in the toplevel router will continue to run. Causing the double render error.

This is why the parent router selector plug must also send a halt.

So in lists:
Subrouter pipeline
Plug a
Plug b
Plug render < halt and return conn

Toplevel router pipeline
Plug a
Plug b
Plug RouterSelelector. < halt and return conn
Plug render # it conn reaches me I am second render

Last Post!

BartOtten

BartOtten

You use a second router, and so risk a second render which (see previous example). That’s the difference.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews