Code Monkey home page Code Monkey logo

Comments (4)

andreapavoni avatar andreapavoni commented on August 23, 2024

I think this is irrelevant. In both cases (subdomain vs url path) you'd need to write some plug that checks either subdomain or url path to get the tenant name on each request.

from apartmentex.

BenMorganIO avatar BenMorganIO commented on August 23, 2024

@shulhi this is all I did:

defmodule App.TenantController do
  plug :set_current_tenant

  defp set_current_tenant(conn, _params) do
    {"host", host} = List.keyfind(conn.req_headers, "host", 0)
    tenant = Tenant.get_by_host!(host)
    assign(conn, :current_tenant, tenant)
  end
end

defmodule App.Tenant do
  def get_by_host!(host) do
    subdomain = host
      |> String.split(".")
      |> Enum.take(1)
      |> Enum.join(".")

    Repo.get_by!(__MODULE__, subdomain: subdomain)
  end
end

from apartmentex.

Dania02525 avatar Dania02525 commented on August 23, 2024

To add to the advice above, the tenant qualification can really come from anywhere- subdomain, path, jwt claim, some other header, etc. basically anything you can access in plug on a request can be used to target a schema

from apartmentex.

shulhi avatar shulhi commented on August 23, 2024

Thanks, I got it working with plug.

from apartmentex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.