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!

Showing Posts 1 to 10

acrolink

acrolink

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

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).

dacz

dacz OP

Yes, I usually use Caddy in production. But because of the service (and it’s variants) I cannot rely on some rewriting on the proxy.

dacz

dacz OP

Yes, I have common pipelines in the basic router (that contains the forward) and then I some specific pipelines on each router (root or domain). According to basic tests it works. I did not include source of the Root and Domain routers here, they are pretty standard (I mean specific pipelines, but nothing unusual).

Just wonder if the RouterSelector is ok way how to do it and not sure about that halt (might be a signal that something is wrong).

BartOtten

BartOtten

My question was more directed at: why not use 1 router with 2 scopes? :slight_smile:

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

dacz

dacz OP

ahaaa. So this halt has to be there because RouterSelector is a Plug? If so this was the missing piece I did not know (together with the fact that halt halts the plugs and not requests.

Thank you!

dacz

dacz OP

How could I specify scope according to the host value from conn? I know that there is :host option in scope but it says that I can specify domains or prefixes. But my problem is that I need one scope for domain.com and other for *.domain.com. And the subdomains are added dynamically - so I cannot list all there.

Did I mis something in the scope options how to do it?

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.

dacz

dacz OP

Ah, true! I missed that possibility. Thank you!

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews