sbaildon

sbaildon

Multi-tenancy with VerifiedRoutes

Hey—I’m looking for some guidance on building multi-tenant-aware paths using VerifiedRoutes.

I have an app that supports multi-tenancy with either a tenant identifier in the path, or custom domains, and the path can optionally contain a list of directly nested entities;

  1. https://my-app.example/tenant-1/parent/child/grandchild
  2. https://tenant-2.example/parent/child/grandchild

Exploding each path into distinct helpers like grandchild_path/3, child_path/2, and parent_path/1 feels like the wrong approach, given that each entity can have many different types of children.

I thought about defining something like this, but I think I’d lose the compile time warnings if I use a list of entities to build ~p

def tenant_path(conn_or_socket, tenant, entities \\ []) when conn_or_socket.host == "my-app.example",
  do: path(conn_or_socket, Router, ~p"/#{tenant}/entities...")

def tenant_path(conn_or_socket, entities \\ []),
  do: path(conn_or_socket, CustomDomainRouter, ~p"/entities...")

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement