/
subdomain_plug.ex
59 lines (44 loc) · 1.02 KB
/
subdomain_plug.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
defmodule ExTenant.SubdomainPlug do
@moduledoc """
-
Example
-------
-
def call(conn, _labels) do
repo = Application.get_env(:ex_tenant, :repo)
conn
|> url_value(:host)
|> ExTenant.TenantUtils.sub_domain()
|> ExTenant.TenantUtils.retrieve_tenant_from_subdomain()
|> repo.put_tenant_id()
conn
end
"""
require Logger
@doc false
defmacro __using__(_opts) do
quote do
import ExTenant.SubdomainPlug, only: [setup: 0, init: 1, call: 2]
def setup() do
end
def init(_opts) do
end
def call(conn, _labels) do
repo = Application.get_env(:ex_tenant, :repo)
conn
|> url_value(:host)
|> ExTenant.TenantUtils.sub_domain()
|> ExTenant.TenantUtils.retrieve_tenant_from_subdomain()
|> repo.put_tenant_id()
conn
end
end
end
@doc false
def url_value(conn, :host) do
quote do
conn = unquote(conn)
conn.host
end
end
end