diff --git a/lib/triplex.ex b/lib/triplex.ex index c126ed4..217f8f0 100644 --- a/lib/triplex.ex +++ b/lib/triplex.ex @@ -235,7 +235,7 @@ defmodule Triplex do end @doc """ - Returns the path for your tenant migrations. + Returns the path for the tenant migrations on your `repo`. """ def migrations_path(repo \\ config().repo) do if repo do diff --git a/lib/triplex/param_plug.ex b/lib/triplex/param_plug.ex deleted file mode 100644 index 026e4f2..0000000 --- a/lib/triplex/param_plug.ex +++ /dev/null @@ -1,25 +0,0 @@ -defmodule Triplex.ParamPlug do - @moduledoc """ - This is a basic plug that loads the current tenant assign from a given - param. - - To plug it on your router, you can use: - - plug Triplex.ParamPlug, - param: :subdomain, - tenant_handler: &TenantHelper.tenant_handler/1 - - See `Triplex.PlugConfig` to check all the allowed `config` flags. - """ - - import Triplex.Plug - alias Triplex.PlugConfig - - @doc false - def init(opts), do: PlugConfig.new(opts) - - @doc false - def call(conn, config), - do: put_tenant(conn, conn.params[config.param], config) -end - diff --git a/lib/triplex/plug_config.ex b/lib/triplex/plug_config.ex deleted file mode 100644 index 2b5c801..0000000 --- a/lib/triplex/plug_config.ex +++ /dev/null @@ -1,48 +0,0 @@ -defmodule Triplex.PlugConfig do - @moduledoc """ - This is a struct that holds all the configuration for triplex plugs. - - Here are the config keys we have: - - - For all plugs - - `tenant_handler`: function to handle te test param. The return of it will - be used as the tenant. - - `assign`: the name of the assign where we must save the tenant. - - For `Triplex.EnsurePlug` - - `callback`: function that might be called when the plug succeeded. It - must return a connection. - - `failure_callback`: function that might be called when the plug failed. - It must return a connection. - - For `Triplex.ParamPlug` - - `param`: the param name to load the tenant from. - - For `Triplex.SessionPlug` - - `session`: the session param name to load the tenant from. - - For `Triplex.SubdomainPlug` - - `endpoint`: the Phoenix.Endpoint to get the host name to dicover the - subdomain. - """ - - defstruct [ - :callback, - :endpoint, - :failure_callback, - :tenant_handler, - - assign: :current_tenant, - param: "tenant", - session: :tenant - ] - - @doc """ - Creates a new `%Plug.Config{}`, normalizing the given `args`. - """ - def new(args \\ []), - do: __MODULE__ |> struct(args) |> normalize_param() - - defp normalize_param(%{param: param} = struct) when is_atom(param), - do: Map.put(struct, :param, Atom.to_string(param)) - defp normalize_param(struct), - do: struct -end - - diff --git a/lib/triplex/ensure_plug.ex b/lib/triplex/plugs/ensure_plug.ex similarity index 71% rename from lib/triplex/ensure_plug.ex rename to lib/triplex/plugs/ensure_plug.ex index 1444aad..7d3fb88 100644 --- a/lib/triplex/ensure_plug.ex +++ b/lib/triplex/plugs/ensure_plug.ex @@ -8,14 +8,14 @@ defmodule Triplex.EnsurePlug do callback: &TenantHelper.callback/2 failure_callback: &TenantHelper.failure_callback/2 - See `Triplex.PlugConfig` to check all the allowed `config` flags. + See `Triplex.EnsurePlugConfig` to check all the allowed `config` flags. """ import Triplex.Plug - alias Triplex.PlugConfig + alias Triplex.EnsurePlugConfig @doc false - def init(opts), do: PlugConfig.new(opts) + def init(opts), do: struct(EnsurePlugConfig, opts) @doc false def call(conn, config), do: ensure_tenant(conn, config) diff --git a/lib/triplex/plugs/ensure_plug_config.ex b/lib/triplex/plugs/ensure_plug_config.ex new file mode 100644 index 0000000..ffc3485 --- /dev/null +++ b/lib/triplex/plugs/ensure_plug_config.ex @@ -0,0 +1,17 @@ +defmodule Triplex.EnsurePlugConfig do + @moduledoc """ + This is a struct that holds the configuration for `Triplex.EnsurePlug`. + + Here are the config keys allowed: + + - `assign`: the name of the assign where we must save the tenant. + - `callback`: function that might be called when the plug succeeded. It + must return a connection. + - `failure_callback`: function that might be called when the plug failed. + It must return a connection. + """ + + defstruct [:callback, :failure_callback, assign: :current_tenant] +end + + diff --git a/lib/triplex/plugs/param_plug.ex b/lib/triplex/plugs/param_plug.ex new file mode 100644 index 0000000..f6a7482 --- /dev/null +++ b/lib/triplex/plugs/param_plug.ex @@ -0,0 +1,32 @@ +defmodule Triplex.ParamPlug do + @moduledoc """ + This is a basic plug that loads the current tenant assign from a given + param. + + To plug it on your router, you can use: + + plug Triplex.ParamPlug, + param: :subdomain, + tenant_handler: &TenantHelper.tenant_handler/1 + + See `Triplex.ParamPlugConfig` to check all the allowed `config` flags. + """ + + import Triplex.Plug + alias Triplex.ParamPlugConfig + + @doc false + def init(opts), do: struct(ParamPlugConfig, opts) + + @doc false + def call(conn, config), + do: put_tenant(conn, get_param(conn, config.param), config) + + defp get_param(conn, %ParamPlugConfig{param: key}), + do: get_param(conn, key) + defp get_param(conn, key) when is_atom(key), + do: get_param(conn, Atom.to_string(key)) + defp get_param(conn, key), + do: conn.params[key] +end + diff --git a/lib/triplex/plugs/param_plug_config.ex b/lib/triplex/plugs/param_plug_config.ex new file mode 100644 index 0000000..0f48a72 --- /dev/null +++ b/lib/triplex/plugs/param_plug_config.ex @@ -0,0 +1,16 @@ +defmodule Triplex.ParamPlugConfig do + @moduledoc """ + This is a struct that holds all configuration for `Triplex.ParamPlug`. + + Here are the config keys allowed: + + - `tenant_handler`: function to handle the tenant param. Its return will + be used as the tenant. + - `assign`: the name of the assign where we must save the tenant. + - `param`: the param name to load the tenant from. + """ + + defstruct [:tenant_handler, assign: :current_tenant, param: "tenant"] +end + + diff --git a/lib/triplex/plug.ex b/lib/triplex/plugs/plug.ex similarity index 83% rename from lib/triplex/plug.ex rename to lib/triplex/plugs/plug.ex index dcf7185..4ea2513 100644 --- a/lib/triplex/plug.ex +++ b/lib/triplex/plugs/plug.ex @@ -8,9 +8,6 @@ defmodule Triplex.Plug do - `Triplex.SessionPlug` - loads the tenant from a session param - `Triplex.SubdomainPlug` - loads the tenant from the url subdomain - `Triplex.EnsurePlug` - ensures the current tenant is loaded and halts if not - - You can also check `Triplex.PlugConfig` for the config options you have for - each plug. """ import Plug.Conn @@ -21,7 +18,11 @@ defmodule Triplex.Plug do Puts the given `tenant` as an assign on the given `conn`, but only if the tenant is not reserved. - See `Triplex.PlugConfig` to the allowed `config` flags. + The `config` map/struct must have: + + - `tenant_handler`: function to handle the tenant param. Its return will + be used as the tenant. + - `assign`: the name of the assign where we must save the tenant. """ def put_tenant(conn, tenant, config) do if conn.assigns[config.assign] do @@ -40,7 +41,9 @@ defmodule Triplex.Plug do @doc """ Ensure the tenant is loaded, and if not, halts the `conn`. - See `Triplex.PlugConfig` to the allowed `config` flags. + The `config` map/struct must have: + + - `assign`: the name of the assign where we must save the tenant. """ def ensure_tenant(conn, config) do if loaded_tenant = conn.assigns[config.assign] do diff --git a/lib/triplex/session_plug.ex b/lib/triplex/plugs/session_plug.ex similarity index 74% rename from lib/triplex/session_plug.ex rename to lib/triplex/plugs/session_plug.ex index 57c1d30..25c95ef 100644 --- a/lib/triplex/session_plug.ex +++ b/lib/triplex/plugs/session_plug.ex @@ -9,15 +9,15 @@ defmodule Triplex.SessionPlug do session: :subdomain, tenant_handler: &TenantHelper.tenant_handler/1 - See `Triplex.PlugConfig` to check all the allowed `config` flags. + See `Triplex.SessionPlugConfig` to check all the allowed `config` flags. """ import Triplex.Plug import Plug.Conn - alias Triplex.PlugConfig + alias Triplex.SessionPlugConfig @doc false - def init(opts), do: PlugConfig.new(opts) + def init(opts), do: struct(SessionPlugConfig, opts) @doc false def call(conn, config), diff --git a/lib/triplex/plugs/session_plug_config.ex b/lib/triplex/plugs/session_plug_config.ex new file mode 100644 index 0000000..0a76ffe --- /dev/null +++ b/lib/triplex/plugs/session_plug_config.ex @@ -0,0 +1,18 @@ +defmodule Triplex.SessionPlugConfig do + @moduledoc """ + This is a struct that holds the configuration for `Triplex.SessionPlug`. + + Here are the config keys allowed: + + - `tenant_handler`: function to handle the tenant param. Its return will + be used as the tenant. + - `assign`: the name of the assign where we must save the tenant. + - `session`: the session param name to load the tenant from. + """ + + defstruct [ + :tenant_handler, + assign: :current_tenant, + session: :tenant + ] +end diff --git a/lib/triplex/subdomain_plug.ex b/lib/triplex/plugs/subdomain_plug.ex similarity index 67% rename from lib/triplex/subdomain_plug.ex rename to lib/triplex/plugs/subdomain_plug.ex index 9fd8d78..e28ed6b 100644 --- a/lib/triplex/subdomain_plug.ex +++ b/lib/triplex/plugs/subdomain_plug.ex @@ -9,24 +9,25 @@ defmodule Triplex.SubdomainPlug do endpoint: MyApp.Endpoint, tenant_handler: &TenantHelper.tenant_handler/1 - See `Triplex.PlugConfig` to check all the allowed `config` flags. + See `Triplex.SubdomainPlugConfig` to check all the allowed `config` flags. """ import Triplex.Plug alias Plug.Conn - alias Triplex.PlugConfig + alias Triplex.SubdomainPlugConfig @doc false - def init(opts), do: PlugConfig.new(opts) + def init(opts), do: struct(SubdomainPlugConfig, opts) @doc false def call(conn, config), do: put_tenant(conn, get_subdomain(conn, config), config) - defp get_subdomain(_conn, %PlugConfig{endpoint: nil}) do + defp get_subdomain(_conn, %SubdomainPlugConfig{endpoint: nil}) do nil end - defp get_subdomain(%Conn{host: host}, %PlugConfig{endpoint: endpoint}) do + defp get_subdomain(%Conn{host: host}, + %SubdomainPlugConfig{endpoint: endpoint}) do root_host = endpoint.config(:url)[:host] if host in [root_host, "localhost", "127.0.0.1", "0.0.0.0"] do nil diff --git a/lib/triplex/plugs/subdomain_plug_config.ex b/lib/triplex/plugs/subdomain_plug_config.ex new file mode 100644 index 0000000..15cf8a7 --- /dev/null +++ b/lib/triplex/plugs/subdomain_plug_config.ex @@ -0,0 +1,19 @@ +defmodule Triplex.SubdomainPlugConfig do + @moduledoc """ + This is a struct that holds the configuration for `Triplex.SubdomainPlug`. + + Here are the config keys allowed: + + - `tenant_handler`: function to handle the tenant param. Its return will + be used as the tenant. + - `assign`: the name of the assign where we must save the tenant. + - `endpoint`: the Phoenix.Endpoint to get the host name to dicover the + subdomain. + """ + + defstruct [ + :endpoint, + :tenant_handler, + assign: :current_tenant, + ] +end diff --git a/test/triplex/plug_config_test.exs b/test/triplex/plug_config_test.exs deleted file mode 100644 index d8541c6..0000000 --- a/test/triplex/plug_config_test.exs +++ /dev/null @@ -1,27 +0,0 @@ -defmodule Triplex.PlugConfigTest do - use ExUnit.Case - - alias Triplex.PlugConfig - - test "new/0 returns a new config with default values set" do - assert PlugConfig.new() == - %PlugConfig{ - callback: nil, - failure_callback: nil, - tenant_handler: nil, - param: "tenant", - assign: :current_tenant, - } - end - - test "new/1 returns a new config and normalize the param" do - assert PlugConfig.new(param: :oi, assign: :ho) == - %PlugConfig{ - callback: nil, - failure_callback: nil, - tenant_handler: nil, - param: "oi", - assign: :ho, - } - end -end diff --git a/test/triplex/ensure_plug_test.exs b/test/triplex/plugs/ensure_plug_test.exs similarity index 100% rename from test/triplex/ensure_plug_test.exs rename to test/triplex/plugs/ensure_plug_test.exs diff --git a/test/triplex/param_plug_test.exs b/test/triplex/plugs/param_plug_test.exs similarity index 100% rename from test/triplex/param_plug_test.exs rename to test/triplex/plugs/param_plug_test.exs diff --git a/test/triplex/plug_test.exs b/test/triplex/plugs/plug_test.exs similarity index 70% rename from test/triplex/plug_test.exs rename to test/triplex/plugs/plug_test.exs index 45c0b89..a5b72da 100644 --- a/test/triplex/plug_test.exs +++ b/test/triplex/plugs/plug_test.exs @@ -4,13 +4,14 @@ defmodule Triplex.PlugTest do import Plug.Conn import Plug.Test alias Triplex.Plug - alias Triplex.PlugConfig + alias Triplex.ParamPlugConfig + alias Triplex.EnsurePlugConfig test "put_tenant/3 must set the tenant to the default assign" do conn = :get |> conn("/") - |> Plug.put_tenant("power", PlugConfig.new()) + |> Plug.put_tenant("power", %ParamPlugConfig{}) assert conn.assigns[:current_tenant] == "power" end @@ -19,7 +20,7 @@ defmodule Triplex.PlugTest do conn = :get |> conn("/") - |> Plug.put_tenant("oi", PlugConfig.new(tenant_handler: handler)) + |> Plug.put_tenant("oi", %ParamPlugConfig{tenant_handler: handler}) assert conn.assigns[:current_tenant] == "olá" end @@ -27,7 +28,7 @@ defmodule Triplex.PlugTest do conn = :get |> conn("/") - |> Plug.put_tenant("power", PlugConfig.new(assign: :tenant)) + |> Plug.put_tenant("power", %ParamPlugConfig{assign: :tenant}) assert conn.assigns[:tenant] == "power" end @@ -36,7 +37,7 @@ defmodule Triplex.PlugTest do :get |> conn("/") |> assign(:current_tenant, "already_set") - |> Plug.put_tenant("power", PlugConfig.new()) + |> Plug.put_tenant("power", %ParamPlugConfig{}) assert conn.assigns[:current_tenant] == "already_set" end @@ -44,7 +45,7 @@ defmodule Triplex.PlugTest do conn = :get |> conn("/") - |> Plug.put_tenant("www", PlugConfig.new()) + |> Plug.put_tenant("www", %ParamPlugConfig{}) assert conn.assigns[:current_tenant] == nil end @@ -52,8 +53,8 @@ defmodule Triplex.PlugTest do conn = :get |> conn("/") - |> Plug.put_tenant("power", PlugConfig.new()) - |> Plug.ensure_tenant(PlugConfig.new()) + |> Plug.put_tenant("power", %ParamPlugConfig{}) + |> Plug.ensure_tenant(%EnsurePlugConfig{}) assert conn.halted == false end @@ -63,8 +64,8 @@ defmodule Triplex.PlugTest do conn = :get |> conn("/") - |> Plug.put_tenant("power", PlugConfig.new()) - |> Plug.ensure_tenant(PlugConfig.new(callback: callback)) + |> Plug.put_tenant("power", %ParamPlugConfig{}) + |> Plug.ensure_tenant(%EnsurePlugConfig{callback: callback}) assert conn.assigns[:test] == "blag" end @@ -74,7 +75,7 @@ defmodule Triplex.PlugTest do conn = :get |> conn("/") - |> Plug.ensure_tenant(PlugConfig.new(failure_callback: callback)) + |> Plug.ensure_tenant(%EnsurePlugConfig{failure_callback: callback}) assert conn.assigns[:test] == "blog" end diff --git a/test/triplex/session_plug_test.exs b/test/triplex/plugs/session_plug_test.exs similarity index 100% rename from test/triplex/session_plug_test.exs rename to test/triplex/plugs/session_plug_test.exs diff --git a/test/triplex/subdomain_plug_test.exs b/test/triplex/plugs/subdomain_plug_test.exs similarity index 100% rename from test/triplex/subdomain_plug_test.exs rename to test/triplex/plugs/subdomain_plug_test.exs diff --git a/test/triplex_test.exs b/test/triplex/triplex_test.exs similarity index 100% rename from test/triplex_test.exs rename to test/triplex/triplex_test.exs